lundi 25 avril 2016

CakePHP 3 - Parent association

I have an existing database (can't make changes) with the following tables:

Category

  • id
  • name

Category hierarchy

  • child_id
  • parent_id

I want to create a checkbox list of (parent)categories for one categorie. I have something like this:

Categorie table

$this->belongsToMany('Categorie', [
        'foreignKey' => 'child_id',
        'targetForeignKey' => 'parent_id',
        'joinTable' => 'category_hierarchy'
    ]);

Categorie controller

$parent = $this->Categorie->Categorie->find('all')
        ->select(['Categorie.id', 'Categorie.naam']);

Now I can create a checkbox list, only the correct categories are not checked. So I do the following:

$categorie = $this->Categorie->get($id, [
        'contain' => ['Categorie']
    ]);

When I do this, the whole form becomes empty. Is it possible to say that it contains parent categories? Something like this:

$categorie = $this->Categorie->get($id, [
        'contain' => ['ParentCategorie']
    ]);




Aucun commentaire:

Enregistrer un commentaire