cakephp 3 queryBuilder for associative data

冷暖自知 提交于 2019-12-11 01:49:08

问题


I have 2 tables.

Table 1:

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

Table 2:

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

In the payment_infos model

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

I have this query:

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

When running the above query I get the following error:

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:


propertyName: The property name that should be filled with data from the associated table into the source table results. By default this is the underscored & singular name of the association so product_price in our example.

As you already have a field name product_price in payment_infos it generate a conflict, I changed the default property name to a custom name.

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

See BelongsTo Associations



来源:https://stackoverflow.com/questions/44281597/cakephp-3-querybuilder-for-associative-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!