Create three Models in the folder app/Model
in its application Cakephp.
Note: Class name is the same name as the file, i.e., class Estado
generates a file Estado.php
and so on. Do not forget to extend (extends
) of AppModel
. All relationships followed the example of Cakephp Framework website.
1) Model Settings:
2) Relationships:
Have only been used $hasMany
which is 1 for many and $belongsTo
which is many for 1, and within the arrays the settings:
There are standard conventions, but particularly I prefer to put mine.
Classes Model:
<?php
//Tabela "Estado" id nomeEstado
class Estado extends AppModel {
public $name = 'estado';
public $useTable = 'estado';
public $primaryKey = 'id';
public $hasMany = array('Cidade' => array('className' => 'Cidade','foreignKey' => 'idEstado'));
}
<?php
//Tabela "Cidade" id idEstado (que busca da tabela estado) nomeCidade
class Cidade extends AppModel {
public $name = 'cidade';
public $useTable = 'cidade';
public $primaryKey = 'id';
public $belongsTo = array('Estado' => array('className' => 'Estado', 'foreignKey' => 'idEstado'));
public $hasMany = array('Participante' => array('className' => 'Participante','foreignKey' => 'idCidade'));
}
<?php
//Tabela "Participante" id Nome id_Cidade
class Participante extends AppModel {
public $name = 'participante';
public $useTable = 'participante';
public $primaryKey = 'id';
public $belongsTo = array('Cidade' => array('className' => 'Cidade', 'foreignKey' => 'idCidade'));
}
Test generated with the classes were successful with the code and figures just below:
foreach($this->Estado->find('all') as $row){
printf('<p>%s %s</p>', $row['Estado']['id'],$row['Estado']['nomeEstado']);
}
foreach($this->Cidade->find('all') as $row){
printf('<p>%s %s %s %s</p>', $row['Cidade']['id'],
$row['Cidade']['nomeCidade'],
$row['Estado']['id'],
$row['Estado']['nomeEstado']);
}
//var_dump($this->Participante->find('all'));
foreach($this->Participante->find('all') as $row){
printf('<p>%s %s %s %s</p>',
$row['Participante']['id'],
$row['Participante']['nome'],
$row['Cidade']['id'],
$row['Cidade']['nomeCidade']);
}
For your doubt you must assemble a variable of type array
with the options as code below:
$options = array('joins' => array(
array(
'table' => 'estado',
'alias' => 'Estado',
'type' => 'INNER',
'conditions' => array('Estado.id = Cidade.idEstado'))
),
'fields' => array('Participante.*', 'Cidade.*', 'Estado.*')
);
foreach($this->Participante->find('all', $options) as $row){
printf('<p>%s %s %s %s %s</p>',
$row['Participante']['id'],
$row['Participante']['nome'],
$row['Cidade']['id'],
$row['Cidade']['nomeCidade'],
$row['Estado']['nomeEstado']);
}
In that link there are more examples.
Hello, welcome to [en.so]. Please include the code you are using, it’s just [Edit] the question. Check out the [Ask] guide for more details.
– brasofilo
Your question is a little confused. Try to rework it.
– ptkato
@Alessandro, in the table Participants I put
idCidade
without the_
to follow the same nomenclature of the others (was id_Cidade, then standardized) and in the example I posted has all the explanation and in the last item the solution. Enjoy!– Maria