I have a problem with declaring conditions for my BTM association - probably missunderstood something.
Imagine a few tables - Notes, NotesEntities, Entities (the last one is not an actual table, but can be any table like Products, Customers, Services, etc…)
In NotesTable there is “entity” field in which there are values like “Services”, “Customers”, etc…
I have set an association in NotesTable like this assuming it will get rows from ServicesTable only if “entity” field in NotesTable = “Services”:
$this->belongsToMany('Services', [ 'foreignKey' => 'note_id', 'targetForeignKey' => 'entity_id', 'joinTable' => 'notes_entities', 'conditions' => ['Notes.entity' => 'Services'] ]);
But the condition doesn’t work and so if I want to fetch a note (e.g. $this->Notes->get()), where entity = “Customers”, it fetches also Services where id of a service = wanted id of a customer. For example: I fetch a Note which has entity = “Customers” and is connected to Customers with ids [1, 2]. That works fine. But when I contain Services, it also fetches Services with ids [1, 2] instead of leaving “services” array empty unless entity = “Services”.
Is there a way to achieve such association? Is it possible to set such global conditions in CakePHP 3.5? I believe I’m not the only one who can use such thing. What am I missing?
The other way around it works. If I fetch a customer and contain Notes, everything is fine and it fetches only notes with entity = “Customers”. But the condition with model “Notes” itelf in NotesTable.php does not work.
Thank you for any answers.