2015-08-07 9 views
0

У меня есть таблица, которая содержит некоторые внешние ключи. Таблица: matriculas. В этой таблице у меня есть внешний ключ для таблицы pessoas, и в этой таблице pessoas имеет внешний ключ к таблице tipopessoas.Как присоединиться к поиску?

Я пытаюсь создать JOIN, чтобы возвращать информацию 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 проекту

enter image description here

ответ

0

Мне это удалось!

Я сделал

Контроллер

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> 
0

Здравствуйте @FernandoPaiva может ли вы обновление вы найти запрос с этим:

$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"), 

          )))); 

Надеется, что это может работало для вас .. .

+0

Спасибо за ваше внимание. Я отредактировал сообщение с тем, что мне нужно. Я пытаюсь выполнить ваше предложение, но не работает. – FernandoPaiva

Смежные вопросы