How to make two relationships for the same table?

1

I have a table named advertencias , in this table I have 2 foreign key for the same table named pessoas in this table person I have two types of Person Student and Teacher, then in table advertencias when I am going to create a new one Warning I have 2 $this->Form->input one for Student and another for Teacher. In the add() function of my controller I can bring the students and add in input of Student but I can not fill the input of Teacher.

How to do this?

My environment looks like this:

Model

class Advertencia extends AppModel {
    public $belongsTo = array(
             //aluno      
            'Pessoa' => array(
                'className' => 'Pessoa',
                'foreignKey' => 'pessoas_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            //professor
            'Pessoa' => array(
                'className' => 'Pessoa',
                'foreignKey' => 'pessoas_id1',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),                
        );

    }

WarningsController

public function add() {
        if ($this->request->is('post')) {
            $this->Advertencia->create();
            if ($this->Advertencia->save($this->request->data)) {
                $this->Session->setFlash(__('The advertencia has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The advertencia could not be saved. Please, try again.'));
            }
        }

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

                //professores
                $this->set("professores", $this->Advertencia->Pessoa->find('list', array(
                                                        'fields' => array("Pessoa.id", "Pessoa.nome", "Pessoa.tipopessoas_id",'Tipopessoa.id','Tipopessoa.descricao'),
                                                        'conditions' => array('Tipopessoa.descricao'=>'PROFESSOR'),
                                                        'recursive' => 0))); 


    }

View

<div class="advertencias form">
<?php echo $this->Form->create('Advertencia'); ?>
    <fieldset>
        <legend><?php echo __('Add Advertencia'); ?></legend>
    <?php
        echo $this->Form->input('data');
                echo $this->Form->input('pessoas_id');//aluno
                echo $this->Form->input('pessoas_id1');//professor
        echo $this->Form->input('disciplinas_id');
        echo $this->Form->input('turmas_id');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Html->link(__('List Advertencias'), array('action' => 'index')); ?></li>
        <li><?php echo $this->Html->link(__('List Disciplinas'), array('controller' => 'disciplinas', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Disciplinas'), array('controller' => 'disciplinas', 'action' => 'add')); ?> </li>
        <li><?php echo $this->Html->link(__('List Turmas'), array('controller' => 'turmas', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Turmas'), array('controller' => 'turmas', 'action' => 'add')); ?> </li>
    </ul>
</div>
    
asked by anonymous 26.08.2015 / 07:55

1 answer

2

Resolved

I did so.

Controller

    public function add() {
            if ($this->request->is('post')) {
                        $this->Advertencia->create();

                    //seta valores para pessoas
                    $this->Advertencia->set(array(
                        "pessoas_id"=>$this->request->data["Advertencia"]["alunos"],
                        "pessoas_id1"=>$this->request->data["Advertencia"]["professores"],
                    ));   

                        if ($this->Advertencia->save($this->request->data)) {
                $this->Session->setFlash(__('The advertencia has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The advertencia could not be saved. Please, try again.'));
                }
            }

                    //alunos
                    $this->set("alunos", $this->Advertencia->Aluno->find('list', array(
                                                                        'fields' => array("Aluno.id", "Aluno.nome", "Aluno.tipopessoas_id",'Tipopessoa.id','Tipopessoa.descricao'),
                                                                        'conditions' => array('Tipopessoa.descricao'=>'ALUNO'),
                                                                        'recursive' => 0))); 

                    //professores
                    $this->set("professores", $this->Advertencia->Professor->find('list', array(
                                                                        'fields' => array("Professor.id", "Professor.nome", "Professor.tipopessoas_id",'Tipopessoa.id','Tipopessoa.descricao'),
                                                                        'conditions' => array('Tipopessoa.descricao'=>'PROFESSOR'),
                                                                        'recursive' => 0))); 

    }

View

<div class="advertencias form">
<?php echo $this->Form->create('Advertencia'); ?>
    <fieldset>
        <legend><?php echo __('Add Advertencia'); ?></legend>
    <?php

                echo $this->Form->input('alunos');//aluno
                echo $this->Form->input('professores');//professor

    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Html->link(__('List Advertencias'), array('action' => 'index')); ?></li>
        <li><?php echo $this->Html->link(__('List Disciplinas'), array('controller' => 'disciplinas', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Disciplinas'), array('controller' => 'disciplinas', 'action' => 'add')); ?> </li>
        <li><?php echo $this->Html->link(__('List Turmas'), array('controller' => 'turmas', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Turmas'), array('controller' => 'turmas', 'action' => 'add')); ?> </li>
    </ul>
</div>
    
27.08.2015 / 06:28