I've created this custom plugin with a CP interface for a website running latest version of Craft 2. Each record in my custom table relates to an entry using an entryId. I've set up all the records and relations with cascade and all and this works perfect. Except for this.

In my PluginModel.php method defineAttributes I have set a key-value pair using AttributeType::Mixed, expecting it would get populated by the BaseModel as the given EntryModel from the model key in the definition:

protected function defineAttributes()
    return array_merge(parent::defineAttributes(), array(
        'entry' => array(AttributeType::Mixed, 'model' => 'EntryModel'),

But when I'm getting my records using findById() and use the result as input to PluginModel::populateModel() the entry value is never populated as an EntryModel. Why?

This is my defineRelations method in my PluginRecord:

public function defineRelations()
    $relations = array(
        'entry'   => array(static::BELONGS_TO, 'EntryRecord', 'entryId', 'onDelete' => static::CASCADE),

    return $relations;

I've searched a lot in this topic, this one, this one and this one, but I simply can't find a reason why it's not working.

Maybe any of you can point me in the right direction to do this? It seems unnecessary to me to do an extra separate EntriesService->getEntryById() call to get the entry based on my plugin records entryId value OR to populate this value of my model manually using EntryModel::populateModel().

Hope you guys can help me out. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.