0

In my project, I have defined a VisibilityBehavior that I apply to some of my models.

<?php

namespace App\Model\Behavior;

use Cake\ORM\Behavior;
use Cake\ORM\Query;

class VisibilityBehavior extends Behavior
{
    protected $_defaultConfig = [
        'implementedFinders' => [
            'visible' => 'findVisible',
        ]
    ];

    public function findVisible(Query $query, array $options)
    {
        $query->where([
            //  various conditions that implement the logic I need
        ]);

        return $query;
    }
}

I have one specific table where I would like to add an additional condition to the WHERE when that behavior is called. Let's say that, only for that specific table, the findVisible method should be something like:

<?php

namespace App\Model\Table;

class SomeTable extends Table
{
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->addBehavior('Visibility');
    }

    public function findVisible(Query $query, array $options)
    {
        $query = parent::findVisible($query);  // to retrieve all the conditions from the "original" method
        $query->andWhere([$this->aliasField('is_archived') => 0]);

        return $query;
    }
}

Obviously, the line with parent doesn't work and throws an error, it's just an example to show what I would like to achieve. Is there any way to do this, that doesn't involve copying the whole findVisible method into my table just to add that extra condition?

mrodo
  • 565
  • 5
  • 21

0 Answers0