0

I have found multiple solutions for CakePHP 2.x (for example here Defining global conditions in Model) and solution for CakePHP 3.x Controller queries: $this->Articles->find('all', ['conditions' => ['domain =' => $this->request->host()]]); but nothing for CakePHP 3.x global query conditions in Models. When i use

function beforeFind($event, $queryData, $options) {
    $queryData['conditions'][]['NOT'][$this->alias . '.parent_id'] = null;
    return $queryData;
}

i get error Cannot use object of type Cake\ORM\Query as array.

Eugen
  • 1,356
  • 12
  • 15

1 Answers1

1

ok, after hours search i have found one very simple solution:

public function beforeFind($event, $query, $options) {  
      $query->where(['field_name' => 'field_value']);
      return $query;
}

or inside initialize() :

class MyClassTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('table_name');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');    

        $this->belongsTo('OtherClass', [
            'foreignKey' => 'class_id',
            'joinType' => 'INNER'
        ])->setConditions(['class_name' => 'any_value']);

    }
}
Eugen
  • 1,356
  • 12
  • 15