beforeFind where datetime is null


#1

Hi
I have a brands and a users table and a join table brands_users. The brands_users table has an additional column “removed” (DATETIME).
In the Users controller I have this function:

public function view()
{
$this->Crud->on(‘beforeFind’, function (Event $event) {
$user = $this->Auth->identify();
$event->subject->query = $this->Users->find()->where([‘id’ => $user[‘id’] ]);
$event->getSubject()->query->contain([
‘Brands’,
‘Models’
]);

});
return $this->Crud->execute();
}

This works but I would like to get only the Brands which don’t have a value in the “removed” column (in the brands_users table).

I tired to implement a beforeFind in the BrandsUsersTable.php file but somehow it’s not working. Maybe it’s not the right approach at all? Or if it is, maybe someone can give me an example of a beforeFind function in a Table?

Thanks!

UPDATE:
I figured it out. Below is a working beforeFind example (see the Type for $options). Also I had to include:

use Cake\Event\EventListenerInterface;
use Cake\Event\Event;

public function beforeFind(Event $event, Query $query, \ArrayObject $options, $primary)    
{
    //Log::write('debug', $query);
    $query->where(['removed IS' => null]); 
}