Crud json-api - self referenced relationship


#1

Hello.

I’m using crud-json-api plugin and got some trouble setting up self-referenced association.

Basically I have folder structure which reference to self as parents and children. In Folders table I have set up following associations:

 $this->belongsTo('Parents', [
     'className' => 'Folders',
     'foreignKey' => 'parent_id',
     'propertyName' => 'parent'
 ]);

 $this->hasMany('Children', [
     'className' => 'Folders',
     'foreignKey' => 'parent_id',
     'propertyName' => 'children'
 ]);

And trying to reach them out through include query parameter. The api returns following response:

A route matching \"array (\n  'controller' => 'Children',\n  '_method' => 'GET',\n  'action' => 'index',\n  'folder_id' => 4,\n  'prefix' => 'v2',\n  'plugin' => NULL,\n  '_ext' => NULL,\n)\" could not be found.

If I change the relationships to look like this:

 $this->belongsTo('Folder', [
     'className' => 'Folders',
     'foreignKey' => 'parent_id',
     'propertyName' => 'parent'
 ]);

 $this->hasMany('Folders', [
     'className' => 'Folders',
     'foreignKey' => 'parent_id',
     'propertyName' => 'children'
 ]);

So router can properly reach controller, response looks like:

A route matching \"array (\n  'controller' => 'Folders',\n  '_method' => 'GET',\n  'action' => 'view',\n  0 => NULL,\n  'prefix' => 'v2',\n  'plugin' => NULL,\n  '_ext' => NULL,\n)\" could not be found.

What’s the right approach to create self referenced relationships?

Also asked on stackoverflow.