Associated model in Cells

Do associations work in cells ?
->contain([‘myAssociatedModel’])
does not work in my cells and return an error message (is not associated).

Associations works everywhere else in my app.
Commenting the line make cell work but I miss the associated data.

Yes they work, could you paste that cumbersome code?

namespace Medias\View\Cell;

use Cake\View\Cell;

/**

  • Affiches cell
    */
    class AffichesCell extends Cell
    {

    /**

    • List of valid options that can be passed into this
    • cell’s constructor.
    • @var array
      */
      protected $_validCellOptions = [];

    /**

    • Default display method.
    • @return void
      */
      public function display($id)
      {
      $this->loadModel(‘Fichiers’);
      $affiches = $this->Fichiers->find()
      ->select([‘Fichiers.id’, ‘Fichiers.dossier_id’, ‘Fichiers.name’, ‘Fichiers.slug’, ‘Fichiers.link’])
      ->where([‘dossier_id’ => $id])
      ->contain([‘Dossiers’])
      ->order([‘Fichiers.sort’ => ‘ASC’])
      ->limit(5)
      ;
      $this->set(‘affiches’, $affiches);
      }
      }

This cell is within Medias plugin.

you have nothing in ->select() from Dossiers, so you need to pass anything in ->select from it or ->enableAutofields(true) or pass whole schema ->select($this->Fichires->Dossiers)

Thank you for trying to help.
Graziel :
I use contain([‘Dossiers’]) in some table methods or in my controller and it works perfectly.
But I tried your way and get the same result :
Warning (512): Could not render cell - Fichiers is not associated with Dossiers [/home/wmcmiinz/www/matapeste_2018/vendor/cakephp/cakephp/src/ORM/EagerLoader.php, line 428] [CORE/src/View/Cell.php, line 271]

can you post your FichiersTable ?

I GOT IT !

Even though the cell is within a plugin, you have to specify the plugin name.
So you must load your model this way : $this->loadModel(‘Medias.Fichiers’);

Hope it helps someone.

Thank you everybody for your time and knowledge.

1 Like

Well, I was wondering if this could be considered almost a bug ?
When you are inside a plugin namespace, you should not have to add the plugin name.
I started with “namespace Medias\View\Cell;” (Medias is the plugin name)
Or maybe I forgot or missed something.

Not quite.

In most cases (plugin extends the core functionality of app), it’s handier when its done this way coz by default plugins are loading app models.