使用DQL在Symfony2 / Doctrine中选择具有子项的对象

时间:2014-02-02 16:26:15

标签: php symfony doctrine dql

我有一个Article和Subcategory实体,我想要,使用Doctrine DQL,选择所有包含1篇或更多文章的子类别,我不想选择空的子类别..我怎么能在一个查询

以下是我的目标:

文章

<?php

namespace Evr\ArticleBundle\Entity;

use Evr\HomeBundle\Entity\ImageThumbnail;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
 * Article
 *
 * @ORM\Table(name="ev_article")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Article{


    /**
     * @var integer
     *
     * @ORM\Column(name="article_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="Evr\HomeBundle\Entity\Subcategory",inversedBy="articles")
     * @ORM\JoinColumn(name="subcategory_id",referencedColumnName="subcategory_id")
     */
    private $subcategory;

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

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

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

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="views", type="integer" , nullable=true)
     */
    private $views;

    /**
     * @var integer
     *
     * @ORM\Column(name="votes", type="integer", nullable=true)
     */
    private $votes;

    /**
     * @var string
     *
     * @ORM\Column(name="photo", type="string", length=255, nullable=true)
     */
    protected $photo;

    /**
     * Image file
     *
     * @var File
     *
     * @Assert\File(
     *     maxSize = "5M",
     *     mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
     *     maxSizeMessage = "The maxmimum allowed file size is 5MB.",
     *     mimeTypesMessage = "Only the filetypes image are allowed."
     * )
     */
    protected $file;
    private $temp;

    public function getAbsolutePath() {
        return (null === $this->photo) ? null : $this->getUploadRootDir() . '/' . $this->photo;
    }

    public function getWebPath() {
        return (null === $this->photo) ? null : $this->getUploadDir() . '/' . $this->photo;
    }

    protected function getUploadRootDir() {
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
    }

    public function getUploadDir() {
        return 'uploads/documents/';
    }

    public function getThumbPath() {
        return 'uploads/documents/thumbs/';
    }

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

    /**
     * Set subcategory
     *
     * @param integer $subcategory
     * @return Article
     */
    public function setSubcategory($subcategory) {

        $this->subcategory = $subcategory;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set type
     *
     * @param integer $type
     * @return Article
     */
    public function setType($type) {
        $this->type = $type;

        return $this;
    }

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

    /**
     * Set content
     *
     * @param text $content
     * @return Article
     */
    public function setContent($content) {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return text 
     */
    public function getContent() {
        return $this->content;
    }

    /**
     * Set exclusive_content
     *
     * @param text $exclusiveContent
     * @return Article
     */
    public function setExclusiveContent($exclusiveContent) {
        $this->exclusive_content = $exclusiveContent;

        return $this;
    }

    /**
     * Get exclusive_content
     *
     * @return text 
     */
    public function getExclusiveContent() {
        return $this->exclusive_content;
    }

    /**
     * Set creation_date
     *
     * @param DateTime $creationDate
     * @return Article
     */
    public function setCreationDate($creation_date) {
        $this->creation_date = $creation_date;

        return $this;
    }

    /**
     * Get creation_date
     *
     * @return DateTime 
     */
    public function getCreationDate() {
        return $this->creation_date;
    }

    /**
     * Set views
     *
     * @param integer $views
     * @return Article
     */
    public function setViews($views = 0) {
        $this->views = $views;

        return $this;
    }

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

    /**
     * Set votes
     *
     * @param integer $votes
     * @return Article
     */
    public function setVotes($votes) {
        $this->votes = $votes;

        return $this;
    }

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

    /**
     * Set photo
     *
     * @param string $photo
     * @return Article
     */
    public function setPhoto($photo) {
        $this->photo = $photo;

        return $this;
    }

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

    /**
     * Set file
     *
     * @param UploadedFile $file
     * @return Article
     */
    public function setFile(UploadedFile $file = null) {
        $this->file = $file;

        return $this;
    }

    /**
     * Get file
     *
     * @return UploadedFile 
     */
    public function getFile() {
        return $this->file;
    }

    /**
     * Called before saving the entity
     * 
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload() {
        if (null !== $this->file) {
            $filename = sha1(uniqid(mt_rand(), true));
            $this->photo = $filename . '.' . $this->file->guessExtension();
        }
    }

    /**
     * Called after entity persistence
     *
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload() {

        if (null === $this->file) {
            return;
        }

        $this->file->move(
                $this->getUploadRootDir(), $this->photo
        );

        $this->file = null;
    }

    /**
     * Called before entity removal
     *
     * @ORM\PostRemove()
     */
    public function removeUpload() {
        if ($file = $this->getAbsolutePath()) {
            if (file_exists($file)) {

                unlink($file);
            }
        }
    }

    public function updateUploadedFile($file) {

        $this->removeUpload();

        $this->file = $file;
        $this->preUpload();
        $this->upload();
    }

}

子类别

<?php

namespace Evr\HomeBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Subcategory
 *
 * @ORM\Table(name="ev_subcategory")
 * @ORM\Entity
 */
class Subcategory
{
    /**
     * @var integer
     *
     * @ORM\Column(name="subcategory_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="Category",inversedBy="subcategories")
     * @ORM\JoinColumn(name="category_id",referencedColumnName="category_id")
     */
    private $category;

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

    /**
     * @ORM\OneToMany(targetEntity="Evr\ArticleBundle\Entity\Article", mappedBy="subcategory")
     */
    protected $articles;

    /**
     * @ORM\OneToMany(targetEntity="Evr\CourseBundle\Entity\Course", mappedBy="subcategory")
     */
    protected $courses;

    public function __construct(){
        $this->articles=new ArrayCollection();
        $this->courses=new ArrayCollection();
    }

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

         /**
     * Set category
     *
     * @param integer $category
     * @return Subcategory
     */
    public function setCategory($category) {

        $this->category = $category;

        return $this;
    }

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

    /**
     * Set subcategory
     *
     * @param string $subcategory
     * @return Subcategory
     */
    public function setSubcategory($subcategory)
    {
        $this->subcategory = $subcategory;

        return $this;
    }

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

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

0 个答案:

没有答案