Relationship between tables causes the error: "Unknown column 'Setores.name' in 'where clause'"

0

I have a relationship between 2 models : Ramais and Setores , and print the data from the 2 on the screen, but I need to make a Search field work, but I was only able to search the table data of Ramais , like name and extension, data of Setores I can not pull from the bank, does anyone have any clue what I am doing wrong?

public function search()
{
    $ramais = $this->paginate($this->Ramais);

    if ($this->request->is('post')) {
        $search = null;
        if (isset($this->request->data['search'])) {
            $search = $this->request->data['search'];
        }

        $ramal = $this->Ramais->find('all',
            [
                'contain' => ['Setores'],

                'conditions'=>['OR'=>
                    [
                        'Ramais.name LIKE'=>'%'.$search.'%',
                        'Ramais.ramal LIKE'=>'%'.$search.'%',
                        'Setores.name LIKE'=>'%'.$search.'%'
                    ]
                ]
            ]
        );

        //debug($ramal); exit();

        $this->set(compact('ramais', 'ramal', 'setor'));
        $this->set('_serialize', ['ramais'], ['setor']);
        $this->render('index');

    }
}

It brings the following error :

  

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Setores.name' in 'where clause'

    
asked by anonymous 17.01.2017 / 14:18

1 answer

-1

To make a filter from a second table, you need to use matching ().

link

Matching will make the inner join of your table accompanied by a condition. Be careful not to get confused with the innerJoinWith () function. It makes it possible to perform the filter however it does not return the values you want to print, that is, it does not add the fields of the second table in the select.

    
24.01.2017 / 22:14