【问题标题】:How to use conditions in self association without joins?如何在没有连接的情况下在自关联中使用条件?
【发布时间】:2015-10-21 10:14:46
【问题描述】:

我有 Product 模型 [id, parent_id, x]。 我想在每个产品上附加单个ProductParent,仅当Product.x = 0Product.parent_id = ProductParent.id 时。

我尝试的是:

public $hasOne = array(
    'ProductParent' => array(
        'className' => 'Product',
        'foreignKey' => false,
        'conditions' => array(
            'ProductParent.id = Product.parent_id',
            'Product.x' => 0
        )
    )
);

并且得到输出Product.parent_id是未知的。

【问题讨论】:

  • 'foreignKey' => false, 为什么要这样做然后设置一个等效的条件?也许从(并添加到问题中)为您提供所需数据的确切 sql 开始。
  • @AD7six 我也尝试了 'foreignKey' => 'parent_id' 并得到相同的结果:Product.x 未知
  • 请将实际的错误消息 - 并执行 sql - 添加到问题中。连同正确的版本一起做你想做的事。

标签: mysql cakephp associations cakephp-2.0


【解决方案1】:

您可以尝试使用树行为。检查这个:http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html

【讨论】:

  • 我怀疑这个答案对 OP 有帮助
猜你喜欢
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多