简体   繁体   English

Cakephp 3.2中两次相同的表模型关联

[英]Same table model association twice in cakephp 3.2

I have done model association in cake 3.2 我在蛋糕3.2中完成了模型关联

Here i have done it for one id of same table . 在这里,我为同一个表的一个ID完成了此操作。

I have tried to do it for other one ,but its not working at all 我已经尝试过为其他人做,但它根本没有用

below is the flow. 下面是流程。

This output i am getting 我得到的输出

{
   "id": 1,
   "publisher_id": 133,
   "user_id": 118,
   "Publisher": {
       "id": 133,
        "name": "Sradha sradha"
    }

Here i want to bind the user id also ,which is belongs to that same user table 在这里我也想绑定用户ID,该用户ID属于同一用户表

The output should come like this(I want to get like this below) 输出应该是这样的(我想在下面这样)

 {
     "id": 1,
     "publisher_id": 133,
     "user_id": 118,
     "Publisher": {
          "id": 133,
          "name": "Sradha sradha"
     }
     "Users": {
         "id": 118,
         "name": "Sradha anjo"
     }

Here both publisher_id and user_id are belongs to same user table . 在这里,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();

Please suggest ,any suggestion will be highly appreciated. 请提出建议,任何建议将不胜感激。

Aliases must be unique 别名必须唯一

What this is doing: 这是做什么的:

$this->AdminRevenues->belongsTo('Users', [ 
    'className' => 'Users', 
    'foreignKey' => 'user_id', 
    'propertyName' => 'Users']);

$this->AdminRevenues->belongsTo('Users', [ 
   'className' => 'Publisher', 
   'foreignKey' => 'publisher_id', 
   'propertyName' => 'Publisher']);

Is declaring an association using AdminRevenues.user_id and then immediately overwriting with an association AdminRevenues.publisher_id . 正在使用AdminRevenues.user_id声明关联,然后立即使用关联AdminRevenues.publisher_id覆盖。 Effectively the first call to belongsTo isn't doing anything. 实际上,第一次调用belongsTo并没有做任何事情。

Association aliases need to be unique, otherwise code such as $foo = $this->AdminRevenues->Users would be ambiguous. 关联别名必须是唯一的,否则$foo = $this->AdminRevenues->Users这样的代码将是模棱两可的。 So, just make the association aliases unique: 因此,只需使关联别名唯一即可:

$this->AdminRevenues->belongsTo('Users', [ 
    'className' => 'Users', 
    'foreignKey' => 'user_id', 
    'propertyName' => 'Users']);

$this->AdminRevenues->belongsTo('Publishers', [ // <--- 
   'className' => 'Users',                      // <---
   'foreignKey' => 'publisher_id', 
   'propertyName' => 'Publisher']);

It work for me in cakephp 3+ 在Cakephp中对我有用3+

Lets suppose you have two table ie 假设您有两个表,即
1) RequestArticles 3) Store 1)RequestArticles 3)商店

We have similiar two model:- 我们有两个相似的模型:

1) RequestArticlesTable 2) StoreTable 1)RequestArticlesTable 2)StoreTable

Here is the association that you need to define in RequestArticlesTable we are joing Store table twice 这是您需要在两次调用存储表的RequestArticlesTable中定义的关联

  public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('request_articles');
    $this->displayField('id');
    $this->primaryKey('id');


    $this->belongsTo( 'yourstore', [
        'foreignKey' => 'store_id',
        'className' => 'Store'
    ]);

    $this->belongsTo( 'fromstore', [
        'foreignKey' => 'from_store_id',
        'className' => 'Store'
    ]);     
}

Now we will join tables in Controller like below and set data to view :- 现在,我们将如下所示在Controller中联接表并设置数据以供查看:-

// query to fetch the data from RequestArticles table and set join for store table twice
$requestArticlesDetaile = $this->RequestArticles->get($id, [
    'contain' => ['yourstore','fromstore']
]);
// set fetched data to view 
$this->set('requestArticlesDetaile', $requestArticlesDetaile);

My solution , call it in the controller in contain 我的解决方案,在容器中的控制器中调用它

$this->belongsTo('Senders', [
    'foreignKey' => 'sender_id',
    'joinType' => 'LEFT',
    'className' => 'Manager.Users'
]);

$this->belongsTo('Receivers', [
    'foreignKey' => 'receiver_id',
    'joinType' => 'LEFT',
    'className' => 'Manager.Users'
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM