cakephp icon indicating copy to clipboard operation
cakephp copied to clipboard

3.1: Call to a member function primaryKey() on null in \vendor\cakephp\cakephp\src\ORM\Rule\ExistsIn.php on line 95

Open stmeyer opened this issue 10 years ago • 4 comments

I get the error Fatal error: Call to a member function primaryKey() on null in \vendor\cakephp\cakephp\src\ORM\Rule\ExistsIn.php on line 95 in CakePHP 3.1. I'm using PHP 5.6.15, IIS 7.5.

The Model:

class DevicesTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function initialize(array $config)
    {
        $this->table('devices');
        $this->displayField('serial_no');
        $this->primaryKey('id');

        $this->belongsTo(
            'Parentdevice',
            [
                'foreignKey' => 'parent_device_id',
                'joinType' => 'LEFT',
                'className' => 'Devices'
            ]
        );
        $this->hasMany(
            'Childdevice',
            [
                'foreignKey' => 'device_id',
                'joinType' => 'LEFT',
                'className' => 'Devices'
            ]
        );
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('serial_no', 'create')
            ->notEmpty('serial_no');

        $validator
            ->requirePresence('palette', 'create')
            ->notEmpty('palette');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['parent_device_id'], 'Devices'));
        return $rules;
    }
}

stmeyer avatar Dec 23 '15 13:12 stmeyer

This line of code

$rules->add($rules->existsIn(['parent_device_id'], 'Devices'));

is referencing an association that does not exist. I will update the code to raise a more useful exception in this scenario.

markstory avatar Dec 23 '15 15:12 markstory

It sometimes can be so easy :+1: Thanks for your work and have some nice Christmas days

stmeyer avatar Dec 23 '15 18:12 stmeyer

Seems like something in 3.1.6 caused this error to show up. After the update I started to get this error.

Had the exact same problem tho, using the original class name instead of the relation name. What did it do before, ignore the rule? Guess an error the right thing then, but a bit more describing one would have saved me some debugging time.

GuidoHendriks avatar Dec 23 '15 19:12 GuidoHendriks

Pull request open now.

markstory avatar Dec 25 '15 16:12 markstory