jeremyharris / cakephp-lazyload

A lazy loader for CakePHP entities.
MIT License
61 stars 10 forks source link

getting the below notice on including the trait #4

Closed sanjib-dev closed 8 years ago

sanjib-dev commented 8 years ago

Only variable references should be returned by reference [ROOT\vendor\jeremyharris\cakephp-lazyload\src\ORM\LazyLoadEntityTrait.php, line 44

I am using Cake 3.2.3

jeremyharris commented 8 years ago

Can you show me a full entity you're including it for? Are you overriding any entity methods? Or any LazyLoadTrait methods?

sanjib-dev commented 8 years ago

Below is my table class:

namespace App\Model\Table;

use App\Model\Entity\Course;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use JeremyHarris\LazyLoad\ORM\LazyLoadEntityTrait;

class CoursesTable extends Table
{
use LazyLoadEntityTrait;
public function initialize(array $config)
    {
        parent::initialize($config);
       $this->belongsTo('UsersAuthors', [
            'className'=>'Users',
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Users', [
            'foreignKey' => 'created_by',
            'joinType' => 'INNER'
        ]);

        $this->belongsTo('CourseDeliveryModes', [
            'foreignKey' => 'course_deliverymode_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('CourseSubjects', [
            'foreignKey' => 'course_subject_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('CourseLanguages', [
            'foreignKey' => 'course_language_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('CourseRegions', [
            'foreignKey' => 'course_region_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('CourseLevels', [
            'foreignKey' => 'course_level_id',
            'joinType' => 'INNER'
        ]);

        $this->addBehavior('Custom');
    }
}

/* this is CustomBehavior.php */
class CustomBehavior extends Behavior
{
public function beforeSave(Event $event, \Cake\Datasource\EntityInterface $entity)
    {
           /* custom code */
  }
}

Also have a CustomEventListener in my project which fires on Model.beforeFind

public function beforeFind(Event $event, Query $query,ArrayObject $options, $primary=false)
    {
        if(!isset($options['showAll']))
        {
            return $query->where([$query->repository()->alias().'.is_deleted'=>0]);
        }
    }
lorenzo commented 8 years ago

The trait must be applied to your entities, not to the table class

sanjib-dev commented 8 years ago

thanks..its working as expected.. totally my fault :)