On 12 March 2014 22:38, Leeland Morgan <[email protected]> wrote:

> 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.
>

Just a note: why are you generating methods at all? You are supposed to
code your entity "logic" according to your needs, and not making a tool
create methods that may not concern your problems.

What I got:
>
> event_participant in database with event_id = null, participant_id = null.
> BUT, it was created, lol. So cascade persist worked.
>
>
The problem here is that you need to define the join columns for the
associations "event" and "participant" as non-null. See
http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html#annref-joincolumn



> What I learned I need to do:
>
> $eventParticipant = (new
> EventParticipant())->setParticipant($participant)->setEvent($event);
>

This is still wrong, as it allows to break your logic, and having
eventparticipants without an event or participant. That doesn't really make
any sense, does it?

Instead, code it like following:

class EventParticipant
{
    // ...
    public function __construct(Participant $participant, Event $event)
    {
        $this->participant = $participant;
        $this->event         = $event;
        $this->registeredTime = new DateTime();
    }

    // ...
}

This will prevent your entity from breaking any logic, and reduces any need
for checks.


> Maybe I'm just crazy and done something wrong? Or hoping for too simple
> use of Doctrine ORM?
>
>
Nothing fancy going on, just stay away from the entity generator if you
don't REALLY need it. The entity generator is a tool, and it is far from
being a silver bullet.
Don't use the generator if you don't really need it (for example, when
reverse-engineering a large legacy DB).

Cheers,

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

-- 
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.

Reply via email to