Relationship between tables causes error: "Unknown column 'Sectors.name' in 'Where clause'"

Asked

Viewed 56 times

0

I have a relationship between 2 models: Ramais and Setores, and print the data of the 2 on the screen, but I need to run a Search field, but I was only able to search the data of the table Ramais, as name and extension, the data of Setores I can’t pull from the bench, someone has some hint of what I’m 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');

    }
}

He brings the following error:

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

  • you set up the class Ramais with the relationship ?

  • Good afternoon, yes I related that way. $this->belongsToMany('Sectors', [ 'foreignKey' => 'ramai_id', 'targetForeignKey' => 'setore_id', 'joinTable' => 'ramais_sectors' ]);

  • The column name exists even in the table Setores?

1 answer

-1


  • 1

    Hi, thanks for the answer. I did a research these days in this matching() function, and it worked I forgot to finish my question here. In this case it looks like this: ->contain(['Sectors']) ->matching('Sectors');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.