如何加入查找?

How to make join with find?

我有一个包含一些外键的 table。 table 是:matriculas。在这个 table 中我有一个 table pessoas 的外键,在这个 table pessoas 中有一个 table [=15= 的外键].

我正在尝试创建 JOIN 到 return 的 tipopessoas 信息,但我做不到。

我该怎么做?

我需要这个

SELECT * FROM pessoas t1
INNER JOIN tipopessoas t2 ON (t1.tipopessoas_id = t2.id)
WHERE t2.descrica = "ALUNO";

我正在尝试这个。

public function add() {
        if ($this->request->is('post')) {
            $this->Matricula->create();
            if ($this->Matricula->save($this->request->data)) {
                $this->Session->setFlash(__('The disciplina has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } 

                        $this->Session->setFlash(__('The disciplina could not be saved. Please, try again.'));

        }


                $this->set("pessoas", $this->Matricula->Pessoa->find("list",array(
                                                                                    "fields"=>array("nome"),
                                                                                    "join"=>array("table"=>"tipopessoas",
                                                                                                  "alias"=>"tipo",
                                                                                                  "type"=>"left",
                                                                                                  "condition"=>array(
                                                                                                                   "Pessoa.tipopessoas_id = " => "tipo.id",
                                                                                                                   "tipo.descricao = " => "ALUNO"
                                                                                                                ))
                                                                            )
                                                                     ));


    }

此处workbench项目

你好@FernandoPaiva 你能更新你的 find 查询吗:

$this->Pessoa->find('all', array("fields" => "Pessoa.*, Tipopessoa.*","conditions" => array("Tipopessoa.descrica" => "ALUNO"),
                           "joins" => array(array(
                                "table" => "tipopessoas",
                                "alias" => "Tipopessoa",
                                "type" => "inner",
                                "condition" => array("Tipopessoa.id" => "Pessoa.tipopessoas_id"),

                           )))); 

希望这对你有用...

我设法解决了!

我做到了

控制器

public function add() {
        if ($this->request->is('post')) {
            $this->Matricula->create();
            if ($this->Matricula->save($this->request->data)) {
                $this->Session->setFlash(__('The disciplina has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } 

                        $this->Session->setFlash(__('The disciplina could not be saved. Please, try again.'));

        }


                $this->set("pessoas", $this->Matricula->Pessoa->find('list', array(
                                                        'fields' => array("Pessoa.id", "Pessoa.nome", "Pessoa.tipopessoas_id",'Tipopessoa.id','Tipopessoa.descricao'),
                                                        'conditions' => array('Tipopessoa.descricao'=>'ALUNO'),
                                                        'recursive' => 0)));                                       


    }

查看

<div class="form-group">   

                            <?php echo $this->Form->input("pessoas_id", array("empty"=>"Escolha uma opção",                                                                          
                                                                                "style"=>"width:200px",                                                                          
                                                                                "class"=>"form-control",                                                                                                                                                    
                                                                                ));?>

                    </div>