How to make two relationships for the same table?

Asked

Viewed 229 times

1

I have a table called advertencias, in this table I have 2 Foreign key for the same table called pessoas in this table person I have two types of person Student and Teacher, then in the table advertencias when I go to create a new warning I have 2 $this->Form->input one for Student and one for Teacher. In function add() from my controller I can bring students and add to input but I can’t fill the input teacher’s.

How to do this ?

My environment is 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' => ''
            ),                
        );

    }

Advertenciascontroller

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>
  • Are you using "Fields" in find->list for any particular reason? Are the models correctly set to $displayField (since you are not using default name)? What is receiving data in the field "teachers"?

  • @Danilomiguel my public $displayField = "nome"; in my Person Model is configured to display the person’s name. In debug() of professores is returning only the teachers correctly, the problem is to throw it to my input

1 answer

2


Solved

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>

Browser other questions tagged

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