1
votes

I have a table X connected to table Y through containing When I want to inner join (matching) this with table Z using hasOne and a function. CakePHP automatically connects to the nonexisting default row through hasOne

  public function initialize(array $config)
{  
    $this->belongsTo('Y', [
        'bindingKey' => 'initialen',
        'foreignKey' => 'initialen'
    ]);

    $this->hasOne('Z');
}

further

   public function search($c)
{

    $query = $this->find('all')->contain('Y')->matching('Z', function ($q) use ($c) {

        return $q->where(['Z.client_ID' => $c]);
    });


    return $query;


}

I am getting error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Z.search_id' in 'on clause'

2

2 Answers

0
votes

If Z.search_id does not exist, you have to specify other foreign key that does exist:

$this->hasOne('Z',[
    'foreignKey' => 'some_key'
]);

More info: CakePHP hasOne association

0
votes

Ok, I found the solution myself

$this->hasOne('Z',[
'foreignKey' => false
]);