cakephp 3 queryBuilder用于关联数据

时间:2017-05-31 09:58:29

标签: cakephp cakephp-3.0 query-builder

我有2张桌子。

表1:

product_prices:
id | price |description |pack |display |created |modified |

表2:

payment_infos:
id |payer |pay_date |product_price_id |product_price |

payment_infos模型中

$this->belongsTo('ProductPrices', [
        'foreignKey' => 'product_price_id',
        'className'  => 'ProductPrices',
]);

我有这个问题:

$query = $this->find('all', [
        'contain' => ['ProductPrices']
]));

运行上述查询时,出现以下错误:

Warning (512): Association property name "product_price" clashes 
with field of same name of table "payment_infos". 
You should explicitly specify the "propertyName" option.
[CORE\src\ORM\Association.php, line 722]

1 个答案:

答案 0 :(得分:3)

propertyName :应将关联表中的数据填充到源表结果中的属性名称。默认情况下,这是强调的&在我们的示例中,关联的单数名称为product_price

由于product_price中的字段名称payment_infos会产生冲突,因此我将默认属性名称更改为自定义名称。

$this->belongsTo('ProductPrices', [
    'foreignKey' => 'product_price_id',
    'className'  => 'ProductPrices',
    'propertyName' => 'prod_price'
]);

请参阅BelongsTo Associations