【问题标题】:Same table model association twice in cakephp 3.2cakephp 3.2 中两次相同的表模型关联
【发布时间】:2016-06-23 10:10:52
【问题描述】:

我在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();

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

【问题讨论】:

  • 你只是把关系的名字和表的名字搞混了。 belongsTo('Publisher', [ 'className' => 'Users',。在查询中相同。

标签: php cakephp cakephp-3.x cakephp-3.2


【解决方案1】:

它在 cakephp 3+ 中对我有用

假设您有两张桌子,即
1) 请求文章 3) 商店

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

1) 请求文章表 2) 存储表

这是您需要在 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);

【讨论】:

  • 用例子也很好的回答。
【解决方案2】:

别名必须是唯一的

这是在做什么:

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

【讨论】:

  • 从 OP 所说的看来,用户和发布者都被引用到同一个 Table 对象,所以我认为在这两种情况下都应该是 'className' =&gt; 'Users'
  • 这里用户与查看者相同,发布者不同。我的意思是 user_id(viewer) 和 publisher_id 属于一个父表用户。由于它属于同一张桌子,我只得到两个中的一个。我需要两个都绑定。
  • @AD7six 这里没有发布者模型,:( :(
  • 这个是对的,你只需要在你的contains中除了Users之外添加Publishers
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多