user5542093
user5542093

Reputation:

cakephp 3 queryBuilder for associative data

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]

Upvotes: 2

Views: 264

Answers (1)

Rayann Nayran
Rayann Nayran

Reputation: 1135

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

Upvotes: 4

Related Questions