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.
What you tried so far?
– Beterraba
You’ll need to create an action in the controller
pessoas
to search the person by CPF, after this, will redirect to the controllerprojetos
-> actionadd
passing theid
of the person as parameter. Basically, this is the path, there are also other forms, using ajax.– Marcelo Aymone