cakephp 3 queryBuilder for associative data
Asked Answered
O

1

2

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]
Odds answered 31/5, 2017 at 9:58 Comment(1)
replace 'className' => 'ProductPrices' with 'joinType' => 'INNER'. Have you used bake console for generating model? Also, What are you trying to achieve?Bowfin
T
4

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

Thickening answered 31/5, 2017 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.