multiple one to many relationships to the same entity

34 views
Skip to first unread message

Damiano Massarelli

unread,
Apr 18, 2017, 5:25:46 AM4/18/17
to redbeanphp
Hi

I'm trying to develop a simple application with Redbeanphp but I'm struggling with multiple one to many relationships to the same entity:
Say I have an entity called "person" and an entity called "animal", I would like to store liked animals and disliked animals for a certain person.

When I try something like this

$person = R::dispense("person");
$cat = R::dispense("animal");
$cat->name = "cat";
$dog = R::dispense("animal");
$dog->name = "doggo";

$person->ownLikedAnimalList[] = $cat;
$person->ownDislikedAnimalList[] = $dog;

I get the following tables
PERSON(id)

ANIMAL(id, name, person_id)

from now on there is no way to tell if an animal is disliked or liked by a certain person because every animal refers to the person table in the same way.


I found questions like this one where a lot of people suggested to use many-to-one relationships but i don't think it is the right way
of doing it: I think it would be correct to have some kind of intermediate table to handle the relationship between a person and its
liked animals and another intermediate relationship for disliked animals.

Thank you

gabor

unread,
Apr 28, 2017, 4:18:11 AM4/28/17
to redbeanphp
Hi,


Yeah I would say: animal - preference - person

$preferences
= $person->ownPreferenceList;
$firstPreference
= reset($preferences);
$firstPreference
->animal;



Something like that.

cheers
Gabor
Reply all
Reply to author
Forward
0 new messages