Reputation: 101
i'd like to set hasOne
and hasMany
to same model
,in a part of my code i need only 1 result, but in other part i need all result (Objects from type Client that will return for a table in my site):
$this->hasOne('Vendas')
->setForeignKey('id_cliente')
->setBindingKey('id')
;
$this->hasMany('Vendas')
->setForeignKey('id_cliente')
->setBidingKey('id');
This is possible, or i made a mistake?
Upvotes: 0
Views: 792
Reputation: 25698
Read the manual https://book.cakephp.org/3.0/en/orm/associations.html. Read the whole page carefully.
Define the class name and conditions you need for your assocs.
$this->hasOne('Foo', [
'className' => 'Foo',
'conditions' => [/* whatever you need*/]
]);
$this->hasMany('Bar', [
'className' => 'Foo',
'conditions' => [/* whatever you need*/]
]);
Upvotes: 1
Reputation: 5748
If the relationship is 1-* you should define the relationship as hasMany()
. Then you write a query for one result and a query for multiple results
Upvotes: 0