Stuck with “model is not associated with model” error ???

Did you already searched the whole internet and tried 100s of solutions ?? and noticeably none of them worked!!! And now you are on the verge of breaking your computer or maybe thinking of praying to god as the last option. Then FEAR NOT,  and if you eventually came here first while you code hunting escapade then lucky you!!! you came to the right place. A few days back I faced the same, I scratched and banged my head to get a clearcut solution for this then I came up with these steps to solve my issue. this should solve your issue and if then praise the lord and his miracles!!!

  1. check that if you are in the right model. the model you are using whether it has any relation to it or not.

Example:

[‘contain’=>’UserData’]

here suppose you are using UserDetails mode to fetch the data, that means you have to have a relation name “UserData” inside your UserDetails model.

2. Use proper syntax to describe the relation

So if you have hasMany relation then at least it should look like this in your UserDetails Model

$this->hasMany(‘UserData’, [
‘className’ => ‘YOUR_PLUGIN_NAME(IF ANY).Users’,
‘foreignKey’ => ‘id’,
‘bindingKey’ => ‘user_id’,
]);

where UserData is not the model name but the relation name and className is the original model class name respective of you model location if it is inside a plugin the write plugin name first.

foreignKey is the id field of your User table and user_id is the field from your current model table.

This should solve your issue.  If not you can send me your code snippet in the comments section.  If I get some time off from building something more awesome (or don’t get kidnapped by the aliens), I will surely revert back to you.

Happy coding to you then !!!

We're Ready, Let's Talk

Error: Contact form not found.