在cakephp 3.2中两次相同的表模型关联
发布时间:2020-12-13 21:55:50 所属栏目:PHP教程 来源:网络整理
导读:我在蛋糕3.2中做了模型关联 在这里,我为同一个表的一个id做了它. 我试图为其他人做这件事,但它根本不起作用 以下是流程. 这个输出我得到了 { "id": 1,"publisher_id": 133,"user_id": 118,"Publisher": { "id": 133,"name": "Sradha sradha" } 这里我想绑定
我在蛋糕3.2中做了模型关联
在这里,我为同一个表的一个id做了它. 我试图为其他人做这件事,但它根本不起作用 以下是流程. 这个输出我得到了 { "id": 1,"publisher_id": 133,"user_id": 118,"Publisher": { "id": 133,"name": "Sradha sradha" } 这里我想绑定用户ID,它也属于同一个用户表 输出应该是这样的(我想在下面这样得到) { "id": 1,"Publisher": { "id": 133,"name": "Sradha sradha" } "Users": { "id": 118,"name": "Sradha anjo" } 这里publisher_id和user_id都属于同一个用户表. $this->AdminRevenues->belongsTo('Users',[ 'className' => 'Users','foreignKey' => 'user_id','propertyName' => 'Users']); $this->AdminRevenues->belongsTo('Users',[ 'className' => 'Publisher','foreignKey' => 'publisher_id','propertyName' => 'Publisher']); $totalAdminRevenue = $this->AdminRevenues->find('all') ->contain([ 'Users' => ['queryBuilder' => function ($q) { return $q->select(['id','name']); }]]) ->toArray(); 请提出建议,任何建议都将受到高度赞赏. 解决方法
别名必须是唯一的
这是做什么的: $this->AdminRevenues->belongsTo('Users','propertyName' => 'Publisher']); 使用AdminRevenues.user_id声明关联,然后立即使用关联AdminRevenues.publisher_id覆盖.有效地第一次调用belongsTo没有做任何事情. 关联别名需要是唯一的,否则代码如$foo = $this-> AdminRevenues->用户将是不明确的.因此,只需使关联别名唯一: $this->AdminRevenues->belongsTo('Users','propertyName' => 'Users']); $this->AdminRevenues->belongsTo('Publishers',[ // <--- 'className' => 'Users',// <--- 'foreignKey' => 'publisher_id','propertyName' => 'Publisher']); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |