I have 3 classes.
(obligatory fields like $id, $name etc are there, just now not mentioning
it)
*Event*
property participating; *@OneToMany(targetEntity="EventParticipant",
mappedBy="participant", cascade={"persist"}) - ArrayCollection*
*Participant*
property participating; *@OneToMany(targetEntity="EventParticipant",
mappedBy="participant", cascade={"persist"}) - ArrayCollection*
*EventParticipant*
property event; *@ManyToOne(targetEntity="Event",
inversedBy="participating")*
property participant; *@ManyToOne(targetEntity="Participant",
inversedBy="participating")*
property registeredTime; - this one is extra field for time of online
registration for event (there are also other extra fields)
As you can see, obviously third class is an Entity for linking two other
entities.
So after doctrine:generate:entities I got in Event and Participant methods
like:
addParticipating(EventParticipant $participating)
removeParticipating(EventParticipant $participating)
etc.
So, what I first tried to do to simplify my usage of adding participants
and removing participants.
This was my little test for it.
$event = new Event()
$em->persist($event);
$participant = new Participant();
$em->persist($participant);
$event->addParticipating(
(new EventParticipant())->setParticipant($participant)
);
$em->flush();
Okay, this was my main hope - I assumed that, since I added cascade, there
is no need for $em->persist($eventParticipant), so I just naively added new
EventParticipant directly to event, with setting participant.
What I got:
event_participant in database with event_id = null, participant_id = null.
BUT, it was created, lol. So cascade persist worked.
What I learned I need to do:
$eventParticipant = (new
EventParticipant())->setParticipant($participant)->setEvent($event);
$em->persist($eventParticipant);
$em->flush();
Okay - I'm good with it as long as it's logical thing to write.
But then - why do classes Event and Participant have methods like
removeParticipating(EventParticipant $participating)? They does not work!
Works only
$em->remove($eventParticipant);
So what's the point in third entity with those "ManyToOne" and "OneToMany"
if I could only GET list of participating but not to do anything else with
it from those entities?
Maybe I'm just crazy and done something wrong? Or hoping for too simple use
of Doctrine ORM?
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.