Cakephp relationship HABTM

Asked

Viewed 541 times

1

I have a question related to Framework Cakephp. Is the following:

Suppose we have a relationship hasAndBelongsToMany (HABTM) between a Model Pessoas and Projetos, with tables of the same name. The Join table is personal projects.

On the table Pessoas, there is the Primarykey id and the countryside Cpf.

In view add.ctp of Projetos, I need to type the Cpf and find the Pessoa for id to make the connection between them. How do I do this?

  • What you tried so far?

  • You’ll need to create an action in the controller pessoas to search the person by CPF, after this, will redirect to the controller projetos -> action add passing the id of the person as parameter. Basically, this is the path, there are also other forms, using ajax.

1 answer

1

I’ve got two dirty ones for you, see which one answers and use it, sure if it works.

Suppose your relationship in Model PROJECTS is like this:

 public $hasAndBelongsToMany = array(
     'Pessoas' => array(
         'joinTable' => 'pessoas_projetos',
         'foreignKey' => 'cpf',                         // Sua FK na tabela de relação
         'associationForeignKey' => 'projeto_id',   // Sua FK na tabela de relação
        'conditions' => array(),
     ),
 );

And your people be like that:

  public $hasAndBelongsToMany = array(
     'Projetos' => array(
         'joinTable' => 'pessoas_projetos',
         'foreignKey' => 'projeto_id',      // Sua FK na tabela de relação
         'associationForeignKey' => 'cpf',  // Sua FK na tabela de relação
        'conditions' => array(),
     ),
 );

Dirt Simple:

As long as your Model is related of the two parts (Both People for Projects, and Projects for People), it is simple, only to query directly in the People Model passing the CPF, more or less like this:

This case is if your query has been made in Model Person

On your controller

$query = array(
    'field' => 'Pessoas.*',
    'conditions' => array(
        'Pessoas.cpf' => $cpf,
    ),
);

$data = $this->Pessoa->find('all', $query);

debug($data);

Soiling less simple:

If you are doing your query in the Project model, you need to create a Join in your query to be able to use this condition.

On your controller:

$query = array(
    'joins' => array(
        array(
            "table" => "pessoas_projetos",
            "alias" => "PessoasProjetos",
            "type" => "LEFT",
            "conditions" => array(
                "Projetos.id = PessoasProjetos.projeto_id"
            )
        ),
    ),
    'field' => '...',
    'conditions' => array(
        'PessoasProjetos.cpf' => $cpf
    ),
);

$data = $this->Projeto->find('all', $query);

debug($data);

Joins is where the relationship is applied when you need to use conditions in the N:N link tables

Remembering that these conditions are valid, only if the Models are correctly related.

  • So how would the add.tcp to add projects searching for people and save in personal table_projects???

Browser other questions tagged

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