Hey,
I am trying to get an object with an association that is not always respected.
$record = $this->Records->find('all')->contain('Users')->first();
I still want to obtain that record even if the user_id is empty.
Thank you for your help!
Hey,
I am trying to get an object with an association that is not always respected.
$record = $this->Records->find('all')->contain('Users')->first();
I still want to obtain that record even if the user_id is empty.
Thank you for your help!
What’s the association between Records and Users?
Records BelongsTo Users
RecordsTable.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'className' => 'Users',
'joinType' => 'INNER',
]);
Should be:
RecordsTable.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'className' => 'Users',
]);
And I think that to achieve what you want the field user_id in the records table will need to be nullable.
And this:
Should be:
Thank you for your help, I edited my code according to your recommandation.
Still having the same null object when user_id is empty even though the user_id field is nullable.
I got it working, it was removing the joinType that did the trick. I don’t know why it didn’t register before
Thank you so much mds2018.
You’re welcome. Glad you got it working mainanthem.