2015-11-09 48 views
0

我使用Symfony 2.我有帶保護變量的實體,它在構造函數中初始化爲ArrayCollection。但是,比我用命令Symfony 2,未定義的變量,受保護的成員在構造函數中初始化爲ArrayCollection通過錯誤,它是未定義的

>php app/console doctrine:fixtures:load -vvv 

我得到錯誤:

[Symfony\Component\Debug\Exception\ContextErrorException] 
Notice: Undefined variable: comments 

Exception trace: 
() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\dctr\src\BlogBundle\Entity\Post.ph 
p:183 
Symfony\Component\Debug\ErrorHandler->handleError() at C:\Bitnami\wampstack-5.5 
.30-0\sym_prog\dctr\src\BlogBundle\Entity\Post.php:183 
BlogBundle\Entity\Post->addComment() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\ 
dctr\src\BlogBundle\DataFixtures\ORM\LoadAuthorData.php:54 
<...> 

//C:\Bitnami\wampstack-5.5.30-0\sym_prog\dctr\src\BlogBu​​ndle\Entity\Post .PHP

/** 
* Add comment 
* 
* @param \BlogBundle\Entity\Comment $comment 
* 
* @return Post 
*/ 
public function addComment(\BlogBundle\Entity\Comment $comment) 
{ 
182 $this->comments[] = $comment; 
183 $comments->setPost($this); 
184 return $this; 
} 

//完整的文件:\ DCTR的\ src \ BlogBu​​ndle \實體\ post.php中

<?php 

namespace BlogBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use Doctrine\ORM\Mapping\Entity; 
use Doctrine\ORM\Mapping\Id; 
use Doctrine\ORM\Mapping\GeneratedValue; 
use Doctrine\ORM\Mapping\Column; 
use Doctrine\ORM\Mapping\OneToMany; 
use Doctrine\ORM\Mapping\ManyToMany; 
use Doctrine\ORM\Mapping\JoinTable; 
use Doctrine\ORM\Mapping\JoinColumn; 
//use Symfony\Component\Validator\Constraints as Assert; 


/** 
* Post 
* 
* @ORM\Table(indexes={@ORM\Index(name="publication_date_idx",columns="publicationDate")}) 
* @ORM\Entity(repositoryClass="BlogBundle\Entity\PostRepository") 
*/ 
class Post 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="title", type="string", length=255) 
    */ 
    private $title; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="body", type="text") 
    */ 
    private $body; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="publicationDate", type="datetime") 
    */ 
    private $publicationDate; 

    /** 
    * @var Comment[] 
    * 
    * @ORM\OneToMany(targetEntity="Comment", mappedBy="post") 
    */ 
    protected $comments; 

    /** 
    * @var Tag[] 
    * 
    * @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts", fetch="EAGER", cascade={"persist"}, orphanRemoval=true) 
    * @ORM\JoinTable(
    *  inverseJoinColumns={@ORM\JoinColumn(name="tag_name", referencedColumnName="name")} 
    *) 
    */ 
    protected $tags; 

    /** 
    * @var PostAuthor 
    * 
    * @ORM\ManyToOne(targetEntity="PostAuthor", inversedBy="posts") 
    */ 
    protected $author; 

    /** 
    * Initializes collections 
    */ 
    public function __construct() 
    { 
     $this->comments = new ArrayCollection(); 
     $this->tags = new ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    * 
    * @return Post 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

    /** 
    * Get title 
    * 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * Set body 
    * 
    * @param string $body 
    * 
    * @return Post 
    */ 
    public function setBody($body) 
    { 
     $this->body = $body; 

     return $this; 
    } 

    /** 
    * Get body 
    * 
    * @return string 
    */ 
    public function getBody() 
    { 
     return $this->body; 
    } 

    /** 
    * Set publicationDate 
    * 
    * @param \DateTime $publicationDate 
    * 
    * @return Post 
    */ 
    public function setPublicationDate($publicationDate) 
    { 
     $this->publicationDate = $publicationDate; 

     return $this; 
    } 

    /** 
    * Get publicationDate 
    * 
    * @return \DateTime 
    */ 
    public function getPublicationDate() 
    { 
     return $this->publicationDate; 
    } 



    /** 
    * Add comment 
    * 
    * @param \BlogBundle\Entity\Comment $comment 
    * 
    * @return Post 
    */ 
    public function addComment(\BlogBundle\Entity\Comment $comment) 
    { 
     $this->comments[] = $comment; 
     $comments->setPost($this); 
     return $this; 
    } 
    /* Another important thing, as already explained, 
    * is that Doctrine only manages the owning side of an association. 
    * This is why we call the setPost() method of the Comment entity 
    * in the addComment() method. This allows persisting 
    * with an association from the inverse side. */ 


    /** 
    * Remove comment 
    * 
    * @param \BlogBundle\Entity\Comment $comment 
    */ 
    public function removeComment(\BlogBundle\Entity\Comment $comment) 
    { 
     $this->comments->removeElement($comment); 
    } 

    /** 
    * Get comments 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getComments() 
    { 
     return $this->comments; 
    } 

    /** 
    * Add tag 
    * 
    * @param \BlogBundle\Entity\Tag $tag 
    * 
    * @return Post 
    */ 
    public function addTag(\BlogBundle\Entity\Tag $tag) 
    { 
     $this->tags[] = $tag; 

     return $this; 
    } 

    /** 
    * Remove tag 
    * 
    * @param \BlogBundle\Entity\Tag $tag 
    */ 
    public function removeTag(\BlogBundle\Entity\Tag $tag) 
    { 
     $this->tags->removeElement($tag); 
    } 

    /** 
    * Get tags 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getTags() 
    { 
     return $this->tags; 
    } 

    /** 
    * Set author 
    * 
    * @param \BlogBundle\Entity\PostAuthor $author 
    * 
    * @return Post 
    */ 
    public function setAuthor(\BlogBundle\Entity\PostAuthor $author = null) 
    { 
     $this->author = $author; 

     return $this; 
    } 

    /** 
    * Get author 
    * 
    * @return \BlogBundle\Entity\PostAuthor 
    */ 
    public function getAuthor() 
    { 
     return $this->author; 
    } 
} 

//sym_prog\dctr\src\BlogBu​​ndle\DataFixtures\ORM\LoadAuthorData.php

line 54 $post->addComment($comment); 

//sym_prog\dctr\src\BlogBu​​ndle\DataFixtures\ORM\LoadAuthorData.php

<?php 

namespace BlogBundle\DataFixtures; 

/* This fixture creates instances 
of Post, PostAuthor, Comment, and CommentAuthor 
and then persists them to the database. 
*/ 

use BlogBundle\Entity\Comment; 
use BlogBundle\Entity\CommentAuthor; 
use BlogBundle\Entity\Post; 
use BlogBundle\Entity\PostAuthor; 
use Doctrine\Common\DataFixtures\Doctrine; 
use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 

/** 
* Author fixtures 
*/ 
class LoadAuthorData implements FixtureInterface 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    public function load(ObjectManager $manager) 
    { 
     $postAuthor = new PostAuthor(); 
     $postAuthor->setName('George Abitbol'); 
     $postAuthor->setEmail('[email protected]'); 
     $postAuthor->setBio('L\'homme le plus classe du monde'); 

     $manager->persist($postAuthor); 

     $post = new Post(); 
     $post->setTitle('My post'); 
     $post->setBody('Lorem ipsum'); 
     $post->setPublicationDate(new \DateTime()); 
     $post->setauthor($postAuthor); 

     $manager->persist($post); 

     $commentAuthor = new CommentAuthor(); 
     $commentAuthor->setName('Kévin Dunglas'); 
     $commentAuthor->setEmail('[email protected]'); 

     $manager->persist($commentAuthor); 

     $comment = new Comment(); 
     $comment->setBody('My comment'); 
     $comment->setAuthor($commentAuthor); 
     $comment->setPublicationDate(new \DateTime()); 

     $post->addComment($comment); 
     $manager->persist($comment); 

     $manager->flush(); 
    } 
} 

回答

0

線183使用$comments帶S,但你的函數接收$comment沒有S.

/** 
* Add comment 
* 
* @param \BlogBundle\Entity\Comment $comment 
* 
* @return Post 
*/ 
public function addComment(\BlogBundle\Entity\Comment $comment) 
{ 
182 $this->comments[] = $comment; 
183 $comments->setPost($this); // Should be $comment->setPost($this); 
184 return $this; 
} 
0

解決的辦法是: C:\ Bitnami \ wampstack-5.5.30-0 \ sym_p ROG \ DCTR的\ src \ BlogBu​​ndle \實體\ post.php中 < ...>

public function addComment(\BlogBundle\Entity\Comment $comment) 
    { 
     181 $this->comments[] = $comment; 
     182 $comment->setPost($this); 
//instead of $comments->setPost($this); 
     183 return $this; 
    } 

上線182我要setPost到$評論的對象,而不是變量$意見。 該方法中不存在註釋變量,它只存在$ this->註釋。
$ this-> comments是一個變量,因此它不能有方法setPost($ this)。

+0

...這就是我說的10分鐘前... – Shady

相關問題