PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] Crud plugin - save many-to-many association

 January 09, 2022     cakephp, cakephp-3.0     No comments   

Issue

I'm currently trying to save associated data with the FriendsOfCake/crud plugin. However, I can't seem to figure out how to save a relation to has a many-to-many association. I have the following code:

$this->Crud->action()->saveOptions(['atomic' => false]);
$this->Crud->listener('relatedModels')->relatedModels(true);
$this->Crud->on('beforeSave', function(\Cake\Event\Event $event){
    $event->subject->entity->Users = [
         ['user_id' => $this->userId]
    ];
});

I have an Albums and Users table that are connected through a join table called UsersAlbums (with album_id and user_id as composite primary key). When I execute the code, the album is stored correctly but the association in the UsersAlbums table isn't saved. I'm currently perform two database calls (one for saving the album and one for saving the association with the user) in a transaction to save the rows. As this is inefficient, is there anyone that does have some suggestions/directions how to solve this by using the crud plugin?


Solution

Try this 8)

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;

class AlbumsController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->viewBuilder()->className('CrudView\View\CrudView');
        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'Crud.Index',
                'Crud.View',
                'Crud.Add',
                'Crud.Edit',
                'Crud.Delete',
            ],
            'listeners' => [
                'CrudView.View',
                'Crud.RelatedModels',
                'Crud.Redirect',
            ],
        ]);
        $this->Crud->action()->config('scaffold.tables_blacklist', [
            'phinxlog',
            'sessions',
            'users_albums',
        ]);
    }

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow([
            'index',
            'view',
        ]);
    }

    public function add()
    {
        $action = $this->Crud->action();
        $action->config('scaffold.fields', [
            'title',
            'description',
        ]);
        $action->config('scaffold.relations', ['Users']);
        $this->Crud->on('beforeSave', function ($event) {
            $event->getSubject()->entity->user_id = $this->Auth->user('id');
        });
        return $this->Crud->execute();
    }
}


Answered By - Zagat Null
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing