In addition to Herman's comment on step 3:

You can have getFavoriteComments() return an array in stead of the 
ArrayCollection:

    public function getFavoriteComments()
    {
        return $this->favoriteComments->toArray();
    }

This way you can't manipulate the ArrayCollection outside of the entity. So you 
_have_ to use the addFavoriteComment() and removeFavoriteComment() methods.

-- 
Jasper N. Brouwer
(@jaspernbrouwer)


On 6 Feb 2014, at 20:48, Herman Peeren <[email protected]> wrote:

> Hi Paul, thank you for your patience. That tutorial is a bit incomplete. It 
> supposes some steps that are not explicitly mentioned. These steps are 
> necessary to execute that piece of code in the example.
> 
> Step 1: making, storing and retrieving a new user
>       * to make a new User: 
> $user = new User();
>       * to persist that new User in the database:
> $em->persist($user);
> $em->flush();
>       * you can now retrieve that user from the database with find( ). The 
> $id is auto-incremented.
> 
> I would also add a $name to the user entity (with a getter and setter), so 
> you can more easily see if everything goes well. Idem I would add a $content 
> or $body or something like that to the comment entity.
> You could read some more about defining your entities in the "getting 
> started" tutorial, after the initial setup: 
> http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html#adding-bug-and-user-entities
>  
> 
> Step 2: Collections in the constructor. 
> A User has some collections: $commentsRead and $commentsAuthored.
> A Comment has a collection of $userFavorites.
> Those collections should be defined in the constructor: 
> 
> // User constructor
> public function __construct()
>     {
>         $this->commentsRead = new ArrayCollection();
>         $this->commentsAuthored = new ArrayCollection();
>     }
>     
> // Comment constructor
> public function __construct()
>     {
>         $this->userFavorites = new ArrayCollection();
>     }
>     
> Even if a User is new, you can now add a Comment to its commentsRead and 
> commentsAuthored collections.
> 
> After those first 2 steps you can now execute that code in the example.
> 
> Step 3: not necessary, but good practice to obey to Demeter's Law
> In the example the add( )  method of the ArrayCollection is used to add 
> something to the collections. That is possible, but considered bad practice: 
> better to only use direct methods of an object (aka "only talk to your direct 
> neighbours"). In order to do so, you should add methods to add and remove to 
> those collections: addFavoriteComment($comment), 
> removeFavoriteComment($comment) etc. See other tutorials, like the getting 
> started mentioned above.
> 
> Step 4: improving the example
> I personally don't like the example with $firstComment very much: someone's 
> first comment is not something you normally would define in a domain model. 
> The tutorial would be better by leaving out such an unrealistic property. 
> I'll try to find some time to rewrite that tutorial a little bit, in order to 
> make it more clear. 
> 
> Hope this helps.
> 
> - Herman

-- 
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/groups/opt_out.

Reply via email to