2017-05-31 88 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