Setting up CakePHP CRUD plugin to use tables from another plugin

in CakePHP


  1. In the controller where you initialise Crud:

    $this->loadComponent('Crud.Crud', [
    // ...
    ]);
    $this->Crud->useModel(sprintf(
        '%s.%s',
        'MyPlugin',
        $this->Crud->getController()->defaultTable
    ));
  2. Prevent linking to the plugin where your tables are:

        /**
         * Before render callback.
         *
         * @param \Cake\Event\Event $event The beforeRender event.
         * @return void
         */
        public function beforeRender(\Cake\Event\EventInterface $event)
        {
            // prevent linking to the table plugin: start
            $viewBuilderVars = $this->viewBuilder()->getVars();
            if (isset($viewBuilderVars['associations'])) {
                foreach ($viewBuilderVars['associations'] as &$association) {
                    foreach ($association as &$associationTable) {
                        $associationTable['plugin'] = null;
                    }
                }
            }
            $this->set('associations', $viewBuilderVars['associations']);
            // prevent linking to the table plugin: end
            if ($this->viewBuilder()->getClassName() === null) {
                $this->viewBuilder()->setClassName('CrudView\View\CrudView');
            }
        }
#cakephp #cakephp-plugin-development