1

I've successfully joined a table to the elements table, however I can't can't seem to add a property so I can use it?

Getting the error Setting unknown property when trying to join table

Entry::find()
    ->addSelect(['craft_event_instances.instance_datetime'])
    ->leftJoin('craft_event_instances', '`craft_event_instances`.`entryId` = `elements`.`id`')

Thanks

1 Answer 1

1

You can't set a property of an element in Craft that does not exists in the field layout, as a behavior or as a public/private attribute in the object. Craft/Yii2 will throw an exception because the property instance_datetime probably does not exist.

In order to attach a custom value you have to either create a new element or you have to store this information somewhere else.

Edit

You could the following as well to fetch your values and store them into a new array

$query = Entry::find();
$columns = $query
    ->addSelect('craft_event_instances.instance_datetime')
    ->innerJoin('craft_event_instances', 'craft_event_instances.entryId = elements.id')
    ->createCommand()
    ->queryAll();
$yourCustomValues = [];
foreach($columns as $key => $column){
    $yourCustomValues[$column['id']] = $column['instance_datetime'];
    unset($columns[$key]['instance_datetime']);
}
$elements = $query->populate($columns);
4
  • thanks for your assistance, is there anyway I can retrofit the relations table somehow?
    – amof
    Mar 22, 2018 at 10:40
  • Yes. You can join the table like every other table as well. Not sure where you problem is Mar 22, 2018 at 11:02
  • Im just having a hard time wrapping my head around the db design. I have a category field called genre which I want included in the results array
    – amof
    Mar 22, 2018 at 11:18
  • It depends what you need... I could explain this on Slack or here in the chat or you could create a new question because it doesn't really fit here in my opinion. Your new question is more about the structure of Craft which is quite simple but you need to understand what it joins. But it would be too much to explain in one comment Mar 22, 2018 at 11:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.