I have to table
1. Products
2. VendorsInventories
NB: Product may contain more than one VendorsInventories.
In Model/Table/VendorsInventories
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('vendors_inventories');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Products', [
'foreignKey' => 'products_id',
'joinType' => 'INNER'
]);
}
And in: Model/Table/ProductsTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('products');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('VendorsInventories',[
'foreignKey' => 'products_id',
'joinType' => 'INNER'
]);
}
in query:
$products=$this->Products->find('all')->contain(['VendorsInventories']);
and the result is:
My main question is: How to seletct rows which contain Relation data. N.B: belongsTo() is working fine, i have problem with hasToMany()
Check the CakePHP Conventions
So, change
to