search

Home  >  Q&A  >  body text

SYMFONY2 LEFTJOIN query error

There are two instances blog and comment:

My query statement:

public function getLatestBlogs($limit=null){
    $qb = $this->createQueryBuilder('b')
        ->select('b','c')
        ->leftJoin('b.comments', 'c')
        ->addOrderBy('b.created', 'DESC')
    ;

    if (false === is_null($limit))
        $qb->setMaxResults($limit);

    return $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
}

Error message:

Error: Call to a member function add() on a non-object in E:\xampp\htdocs\symfony\vendor\doctrine\orm\lib\Doctrine\ORM\PersistentCollection.php line 177
500 Internal Server Error - FatalErrorException 

Commment the main code, the others are generated

/**
 * Class Comment
 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\CommentRepository")
 * @ORM\Table(name="comment")
 * @ORM\HasLifecycleCallbacks()
 */
class Comment{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string",length=100)
     */
    protected $user;

    /**
     * @ORM\Column(type="text")
     */
    protected $comment;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $approved;

    /**
     * @ORM\ManyToOne(targetEntity="Blog",inversedBy="comments")
     * @ORM\JoinColumn(name="blog_id",referencedColumnName="id")
     */
    protected $blog;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $updated;

 }

Blog main code:

class Blog{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string")
 */
protected $title;

/**
 * @ORM\Column(type="string",length=100)
 */
protected $author;

/**
 * @ORM\Column(type="text")
 */
protected $blog;

/**
 * @var
 * @ORM\Column(type="string",length=20)
 */
protected $image;

/**
 * @var
 * @ORM\Column(type="text")
 */
protected $tags;

/**
 * @ORM\OneToMany(targetEntity="Comment",mappedBy="blog")
 */
protected $comments = array();

/**
 * @var
 * @ORM\Column(type="datetime")
 */
protected $created;

/**
 * @var
 * @ORM\Column(type="datetime")
 */
protected $updated;
習慣沉默習慣沉默2756 days ago557

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 16:46:35

    You have to post the code of your blog and comment categories, at least include the comments on the blog.

    UPDATE1:

    The default value of the comments attribute on your Blog class is an empty array, but Doctrine actually uses ArrayCollection. The add() method in your error message is provided by ArrayCollection.

    use Doctrine\Common\Collections\ArrayCollection;
    
    // 在Blog的constructor里给comments一个默认值:
    public function __construct()
    {
        $this->comments = new ArrayCollection();
    }
    

    Source code:

    https://github.com/doctrine/collections/blob/master/lib/Doctrine/Common/Collections/ArrayCollection.php

    UPDATE2:

    Doctrine is an ORM, so it has an object-oriented interface both inside and outside.

    reply
    0
  • Cancelreply