脚本宝典收集整理的这篇文章主要介绍了CakePHP 3 – 用户所属的ToMany用户,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
起点看起来像这样?
// UsersTable $this->belongsToMany('Users',[ 'through' => 'Connections',]);
您的用户表:
class UsersTable extends Table { public function inITialize(array $config) { $this->hasmany('ToConnections',[ 'classname' => 'Connections','foreignKey' => 'user_to' ]); $this->hasMany('FromConnections','foreignKey' => 'user_from' ]); } }
和你的连接表:
class ConnectionsTable extends Table { public function initialize(array $config) { $this->belongsTo('ToUsers',[ 'className' => 'Users','foreignKey' => 'user_to' ]); $this->belongsTo('FromUsers','foreignKey' => 'user_from' ]); } }
然后,您可以根据需要使用contains()加载关联的模型.
$query = $conections->find()->contain([ 'ToUsers','FromUsers' ]); $recipients = TableRegistry::get('users'); $query = $recipients->find()->contain([ 'ToConnections.FromUsers',]);
以上是脚本宝典为你收集整理的CakePHP 3 – 用户所属的ToMany用户全部内容,希望文章能够帮你解决CakePHP 3 – 用户所属的ToMany用户所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。