2

I have two custom entities; A, B.

In terms of database relationships, I want A to have many B.

I find I am able to define a reference_entity field in the entity's baseFieldDefinition(). However, because this just defines a single column in the database my relationships are back to front:

I can give B a reference to A and then use $B->getEntity()->referencedEntities() to get a B's associated A, but I can't do it the other way round because there's just a single id for each row.

Is there a correct way to query or set up these kinds of relationships 'out-the-box' or is a better solution to add my own method to the A entity getReferencedBs() and query the B table for references to A.

Many thanks

  • 1
    I think that you should create your custom method that uses a query. – Eyal Mar 8 '17 at 17:18
1

Sounds like you need to add setCardinality(-1) to your B reference field definition in your A entity. This will change the B reference field to support an unlimited number of references instead of 1, the default cardinality.

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.