How to make a JOIN using find ()?

0

I'm trying to make a join of tables using cakephp's find (). I have 3 tables, they are: users, pessoas e matriculas . In the users table I have a foreign key for the pessoas table and on the matriculas tbm table I have a foreign key for pessoas .

I want to do a JOIN between these tables, I'm trying but when I do the JOIN for the table matriculas the return comes empty. I do not know what it can be if I'm doing it wrong. How to do this?

I'm trying like this.

public function doLogin(){
            $this->autoRender = false;
            $json = $this->request->input("json_decode", true);
            $email = $json["User"]["email"];
            $senha = $json["User"]["senha"];

            $sql["conditions"] = array(
                "User.email"=>$email,
                "User.senha"=>$senha,
                "User.status"=>1
            );
            $sql["joins"] = array(
                array(
                    "table"=>"matriculas",
                    "alias"=>"m",
                    "type"=>"INNER",
                    "conditions"=>array("Pessoa.id"=>"m.pessoas_id")
                )
            );

            $users = $this->User->find('all', $sql);

            $array;
            if($users){
                $array = array("status"=>"1", "msg" => "Login efetuado", "result"=>$users);
            }else{
                $array = array("status"=>"0", "msg" => "Usuário ou senha inválido", "result"=>$users);
            }

            return json_encode($array);
        }

Empty result

{"status":"0","msg":"Usu\u00e1rio ou senha inv\u00e1lido","result":[]}

My ERM project

    
asked by anonymous 24.08.2015 / 23:58

1 answer

1

The way you model this Join , is from the previous version to 2.x.x!

Correct would look like this:

...

$conditions = array(

    'conditions' =>  array(

        'User.email' => $email,
        'User.senha' => $senha,
        'User.status' => 1

    ),

    'joins' => array(

        array(
            'table' => 'matriculas',
            'alias' => 'm',
            'type' => 'INNER',
            'conditions' => array(
                'Pessoa.id' => 'm.pessoas_id'
            )
        ),

    ),

);


$users = $this->User->find('all', $conditions);

...
    
26.08.2015 / 02:39