Associations-associação da tabela(relacionamento recurssivo, auto relacionamento)

Alo pessoal
Pertenço a uma raca antiga na informática e agora resolvi estudar o Cake, pois acho uma ferramenta excepcional.
Porém em certa altura resolvi modelar um tabela com associação recurssiva e não consigo trabalhar com ela no Apache devido a erros causados pelo fato de usar o mesmo “ALIAS”.
Lendo a documentação (, descobri que é possível resolver isso (ad hoc), mas tentei a forma que documentaram mas nao funcionou, que trabalhando o código do objetos TABLE, gerado pelo Cake.
Caso alguem ja tenha passado por esse mesmo problema e tenha uma luz, peço que reporte para q eu possa dar continuidade ao estudo.

Hi folks,
I belong to an ancient race in computer science and now I have decided to study Cake, because I find it an exceptional tool. However at some point I decided to model a table with recursive association and I can not work with it in Apache due to errors using the same “ALIAS”. Reading the documentation (, I discovered that it is possible to solve this (ad hoc), but I tried the way they documented but it did not work, that working the TABLE object code, generated by Cake. If someone has already had this problem and has a light, I ask you to report so that I can continue the study. Thank you

Please share the code you’ve tried.

I created MYSql DB table scripting like this:
use test;
child_id INT NULL,
PRIMARY KEY (father_id),
CONSTRAINT fk_Father_Father
FOREIGN KEY (child_id)
REFERENCES test.Peoples (father_id)

After that build the parts of the MVC with cake bake.
I opened the browser in the correct address and received this message:

So, I tryed other orientation like is wrote on docs:

class PeoplesTable extends Table
* Initialize method
* @param array $config The configuration for the Table.
* @return void
public function initialize(array $config)
$this->hasMany(‘child_id’, [
‘className’ => ‘Peoples’

              $this->belongsTo('father_id', [
              'className' => 'Peoples'

and… nothing. The wrong is : The Peoples association is not defined on Peoples. :frowning:

What’s the database schema here? Your Peoples table looks like it’s a join table to connect people, which are stored in another table? What’s that other table called?

Ok… sorry.
This is the same schema.

I built faster like example for you view.

Both is same schema.

I want to demonstrate the problem…


This is a recursive association…
The table with it self.

Okay, so, having child_id in there means that it’s a one-to-one relation, not one-to-many. A person can have only one father seems right, but can they have only one child, or lots of children? If the latter, then you don’t want child_id in the schema at all; if the former, then hasMany is the wrong association to be using.

Also, your hasMany and belongsTo association definitions are wrong. The first parameter is not the field to use, it’s the name to give the association, like Fathers and Children, and the field can be specified in the options; check the manual for specifics on those.

I’ll testing.

Tks a lot.

That FatherTable class was generate by Cake, originaly, when I used bake command.

PersonsTable…not FatherTable

Which manual do you refer to?

It seems like there’s issues starting with your database setup, so it’s maybe not surprising that the code bake generated isn’t right.

  1. Your table should be named as the plural, which is just people, not peoples.
  2. Seems that child_id probably shouldn’t be there, in order to have a one-to-many relation.

I’d recommend fixing those two things first, then re-bake the table. Then see the documentation on associations, particularly the sub-categories example about self-associated tables. If it’s still not working at that point, then come back with your updated schema and initialize function, and we’ll go from there.