2017-05-31 84 views
2

我有2个表。cakephp 3用于关联数据的queryBuilder

表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] 
+0

取代' '的className'=> 'ProductPrices''与'' joinType '=>' INNER''。您是否使用烘焙控制台生成模型?另外,你想达到什么目的? – Sam

回答

2

propertyName的:应该装满从关联表数据为源表结果的属性名称。默认情况下,这是&的单数名称,在我们的示例中为product_price

因为您已经有一个字段名称product_pricepayment_infos它会产生冲突,我将默认属性名称更改为自定义名称。

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

参见BelongsTo Associations