3.1: Call to a member function primaryKey() on null in \vendor\cakephp\cakephp\src\ORM\Rule\ExistsIn.php on line 95
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;
}
}
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.
It sometimes can be so easy :+1: Thanks for your work and have some nice Christmas days
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.
Pull request open now.