TransWikia.com

Plugin: Record relationship with Entry

Craft CMS Asked on April 12, 2021

I’m trying to build a plugin that has relations with Craft entries.

The plugin db table has field called entryId that defines a foreign key to the {{%elements}} table. Then my Record class extends ActiveRecord and set the relationship like this:

public function getEntry()
{
    return $this->hasOne(Element::class, [
        'id' => 'entryId'
    ]);
}

In the service class I can get the related record doing so:

$record = MyRecord::findOne([
       'entryId' => $entryId
]);
var_dump($record->entry);die;

The relationship seems working fine… anyway I think I’m relating to the wrong model. I mean… I would like to get the entry with title, slug and other properties, instead I get and object of data from the elements db table.

    object(craftrecordsElement)#227 (10) {
    ["_attributes":"yiidbBaseActiveRecord":private]=>
  array(11) {
    ["id"]=>
    int(48)
    ["draftId"]=>
    NULL
    ["revisionId"]=>
    NULL
    ["fieldLayoutId"]=>
    int(1)
    ["type"]=>
    string(20) "craftelementsEntry"
    ["enabled"]=>
    int(1)
    ["archived"]=>
    int(0)
    ["dateCreated"]=>
    string(19) "2020-07-30 11:29:10"
    ["dateUpdated"]=>
    string(19) "2020-08-04 19:38:57"
    ["dateDeleted"]=>
    NULL
    ["uid"]=>
    string(36) "85c2f79c-5ee7-434d-8c29-d3d807a64b6c"
  }

Can anyone point me where I’m wrong? Thanks for help!

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP