Symfony 2,未定义的变量,在构造函数中初始化为 ArrayCollection 的受保护成员通过错误,它是未定义的

Posted

技术标签:

【中文标题】Symfony 2,未定义的变量,在构造函数中初始化为 ArrayCollection 的受保护成员通过错误,它是未定义的【英文标题】:Symfony 2, Undefined variable, protected member initialized in constructor as ArrayCollection throughs error, that it is undefined 【发布时间】:2015-11-09 12:24:31 【问题描述】:

我使用 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\Entity\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('gabitbol@example.com');
        $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('dunglas@gmail.com');

        $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();
    

【问题讨论】:

【参考方案1】:

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

【讨论】:

【参考方案2】:

解决办法是: C:\Bitnami\wampstack-5.5.30-0\sym_prog\dctr\src\BlogBu​​ndle\Entity\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 设置为 $comment 对象,而不是 $cmets 变量。 此方法中不存在注释变量,它仅作为 $this->cmets 存在。 $this->cmets 是一个变量,因此它不能有方法 setPost($this)。

【讨论】:

以上是关于Symfony 2,未定义的变量,在构造函数中初始化为 ArrayCollection 的受保护成员通过错误,它是未定义的的主要内容,如果未能解决你的问题,请参考以下文章

请问java构造函数如何初始化成员变量的啊?

c++中静态成员变量和静态成员函数(笔试经历)

程序没有按顺序通过构造函数,从而导致未初始化的变量

cppcheck警告:变量未在带有初始化初始化的构造函数中实例化

Kotlin类的初始化 ② ( 主构造函数 | 主构造函数定义临时变量 | 主构造函数中定义成员属性 | 次构造函数 | 构造函数默认参数 )

c#中对象初始化是啥意思?