5

我在 cake 3.2 中做了模型关联

在这里,我已经为同一张表的一个 id 完成了它。

我试图为其他人做,但它根本不起作用

下面是流程。

我得到的这个输出

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

在这里我也想绑定用户ID,它属于同一个用户表

输出应该是这样的(我想在下面得到这样的)

 {
     "id": 1,
     "publisher_id": 133,
     "user_id": 118,
     "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();

请提出建议,任何建议将不胜感激。

4

3 回答 3

8

别名必须是唯一的

这是在做什么:

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

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

使用关联声明关联AdminRevenues.user_id,然后立即用关联覆盖AdminRevenues.publisher_id。实际上,第一次调用belongsTo并没有做任何事情。

关联别名必须是唯一的,否则诸如此类的代码$foo = $this->AdminRevenues->Users将是模棱两可的。因此,只需使关联别名唯一:

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

$this->AdminRevenues->belongsTo('Publishers', [ // <--- 
   'className' => 'Users',                      // <---
   'foreignKey' => 'publisher_id', 
   'propertyName' => 'Publisher']);
于 2016-06-23T11:16:04.283 回答
2

它在 cakephp 3+ 中对我有用

假设您有两个表,即
1) RequestArticles 3) Store

我们有类似的两个模型:-

1) RequestArticlesTable 2) StoreTable

这是您需要在 RequestArticlesTable 中定义的关联,我们将加入 Store 表两次

  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'
    ]);     
}

现在我们将在 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);
于 2017-03-09T09:53:31.113 回答
0

我的解决方案,在包含的控制器中调用它

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

$this->belongsTo('Receivers', [
    'foreignKey' => 'receiver_id',
    'joinType' => 'LEFT',
    'className' => 'Manager.Users'
]);
于 2018-11-14T18:30:04.093 回答