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

Monday, February 21, 2022

[FIXED] Containable behavior is not working across the Plugins in Cakephp 3.0

 February 21, 2022     cakephp-3.0, orm, php     No comments   

Issue

I am working on cakephp 3.0 and i like to use plugins and i wrote my application in the form of plugins and my problem is that cakephp 3.0 doesn't allow containable behavior across the plugin and it's show Model is not associated

I have created two plugins Auth and Comments:

Controllers in Auth Plugin

  • UsersController
  • RolesCOntroller

Models in Auth Plugin

In cakephp 3.0 models are changes as compare to 2.x they have Entity and ModelTable so Auth Model Tables are below

  • UsersTable
  • RolesTable

UsersTable:-

            <?php
        namespace Auth\Model\Table;

        use Auth\Model\Entity\User;
        use Cake\ORM\Query;
        use Cake\ORM\RulesChecker;
        use Cake\ORM\Table;
        use Cake\Validation\Validator;


        class UsersTable extends Table
        {
            public function initialize(array $config)
            {
                parent::initialize($config);

                $this->table('users');
                $this->displayField('name');
                $this->primaryKey('id');

                $this->addBehavior('Timestamp');

                $this->belongsTo('Roles', [
                    'foreignKey' => 'role_id',
                    'joinType' => 'INNER',
                    'className' => 'Auth.Roles'
                ]);
                $this->hasMany('Comments', [
                    'foreignKey' => 'user_id',
                    'className' => 'Auth.Comments'
                ]);
                $this->hasMany('Contents', [
                    'foreignKey' => 'user_id',
                    'className' => 'Auth.Contents'
                ]);
            }


            public function validationDefault(Validator $validator)
            {
                return $validator;
            }


            public function buildRules(RulesChecker $rules)
            {
                $rules->add($rules->isUnique(['email']));
                $rules->add($rules->isUnique(['username']));
                $rules->add($rules->existsIn(['role_id'], 'Roles'));
                return $rules;
            }}

RolesTable

        <?php
        namespace Auth\Model\Table;

        use Auth\Model\Entity\Role;
        use Cake\ORM\Query;
        use Cake\ORM\RulesChecker;
        use Cake\ORM\Table;
        use Cake\Validation\Validator;


        class RolesTable extends Table
        {

            public function initialize(array $config)
            {
                parent::initialize($config);

                $this->table('roles');
                $this->displayField('id');
                $this->primaryKey('id');

                $this->addBehavior('Timestamp');

                $this->hasMany('Users', [
                    'foreignKey' => 'role_id',
                    'className' => 'Auth.Users'
                ]);
            }

            public function validationDefault(Validator $validator)
            {


                return $validator;
            }
        }

CommentsPlugin

Controllers in Comments Plugin

  • CommentsController

Models in Comments Plugin

  • CommentsTable

        <?php
        namespace Comments\Model\Table;
    
        use Cake\ORM\Query;
        use Cake\ORM\RulesChecker;
        use Cake\ORM\Table;
        use Cake\Validation\Validator;
        use Comments\Model\Entity\Comment;
    
        class CommentsTable extends Table
        {
    
            public function initialize(array $config)
            {
                parent::initialize($config);
    
                $this->table('comments');
                $this->displayField('name');
                $this->primaryKey('id');
    
                $this->addBehavior('Timestamp');
                $this->addBehavior('Tree');
    
                $this->belongsTo('ParentComments', [
                    'className' => 'Comments.Comments',
                    'foreignKey' => 'parent_id'
                ]);
                $this->belongsTo('Users', [
                    'foreignKey' => 'user_id',
                    'className' => 'Comments.Users'
                ]);
                $this->belongsTo('Contents', [
                    'foreignKey' => 'content_id',
                    'joinType' => 'INNER',
                    'className' => 'Comments.Contents'
                ]);
                $this->hasMany('ChildComments', [
                    'className' => 'Comments.Comments',
                    'foreignKey' => 'parent_id'
                ]);
            }
    
            public function validationDefault(Validator $validator)
            {
    
                return $validator;
            }
    
            public function buildRules(RulesChecker $rules)
            {
                $rules->add($rules->isUnique(['email']));
                $rules->add($rules->existsIn(['parent_id'],       'ParentComments'));
                $rules->add($rules->existsIn(['user_id'], 'Users'));
                $rules->add($rules->existsIn(['content_id'], 'Contents'));
                return $rules;
            }
        }
    

Now i wanna get all users with their related data in comments controller and i am doing like this :

    $this->Comments->Users->find('all',[
       'contain'=>['Roles']
    ])

and it's show's error Users is not associated with Roles but it's working in old version of cakephp(2.7)

Users is not associated with Roles


Solution

There is no containable behavior anymore, contain is now part of the ORM, and it generally works fine with plugin models/associations. I'd suggest to start debugging what the very specific error message tells you, check why Comments.Users and Comments.Contents are using auto-tables (instances of \Cake\ORM\Table) instead of concrete instances of your table classes! To me it looks like you'd want to use Auth.Users instead of Comments.Users .

I copied this content from first comment of the question writen by @ndm



Answered By - Muhammad Asif Saleem
  • 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