More about adding collection management methods later in the same tutorial, 
under 9.4:
http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html#association-management-methods

On Thursday, 6 February 2014 20:48:23 UTC+1, Herman Peeren 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*
>
> On Thursday, 6 February 2014 18:55:38 UTC+1, paul kendal wrote:
>>
>>
>>
>> Hi
>>
>> i am new to doctrine and trying to learn from the tutorial here    
>> doctrine-project 
>> : 9.1. Association Example 
>> Entities<http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html>.
>>  i followed the example of the tutorial and created two classes: Users and 
>> Comment. (there was a slight problem with the coding of the classes but 
>> this was resolved in another post; in summary; i had to . 
>>
>> my two classes are listed here: a gist account:  
>> <https://gist.github.com/anonymous/8848863>
>>
>> i am now trying following the second part of the tutorial in which the 
>> author *Establishing Associations*   . i.e: 
>> <?php
>> $user = $em->find(’User’, $userId);
>> // unidirectional many to many
>> $comment = $em->find(’Comment’, $readCommentId);
>> $user->getReadComments()->add($comment);
>> $em->flush();
>> // unidirectional many to one
>> $myFirstComment = new Comment();
>> $user->setFirstComment($myFirstComment);
>> $em->persist($myFirstComment);
>> $em->flush();
>>
>>
>> my problem however is that when i try to do the above code, i now get 
>> this message: 
>>
>> *Fatal error: Call to a member function getReadComments() on a non-object 
>> *
>>
>> i suspect that the problem occurred because there are no values in the 
>> comments table etc. but i dont know how to put the values into the table. 
>> i.e it cannot be done manually beucase there is a constraint on the tables. 
>>
>> i would really appricaite the amendment of the code above to populate all 
>> the table with intial values. 
>>
>> sorry to ask this question but i am still trying to get to grips with how 
>> doctrine works. 
>>
>> warm regards 
>>
>> Paul 
>>
>

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