How to filter object listing using a metadata (object_metadata table entry) field?

Hello there!

I have two class definitions, Person and Course.

On Course, I’ve added Advanced Many-To-Many Object Relation called persons for Person objects, with a multiselect metadata field called roles (as Person can be, simultaneously, a lecturer, examiner, tutor,…).

Now I need to get Course objects for a single Person filtered by a specified role (the metadata field). Something along the lines of:

$person->getCourses(['role_lecturer']);

Normally, I would do something like this (or just use the Reverse Many-To-Many Object Relation):

$listing = new Course\Listing();
$listing->setCondition('persons LIKE ?', '%,' . $id . ',%');

But I can’t do that, as I’ve got my role metadata field to worry about now. I can filter out the roles “manually” after getting the data from the database, but that’s really clunky and inefficient. I can’t use dependencies either.

It would seem like joining the object_metadata table when building the listing would be the most efficient solution. But I don’t even know if that’s possible.

So, in short, how can I filter objects in a listing by their metadata fields? I can’t find a word on this in the documentation, so any help is appreciated. Thanks!

did you have a look at Listing\Dao::addJoins ?

1 Like

I have, now. I’ll try and make it work, thank you very much!