Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用DQL在Symfony2/doctor中选择具有子对象的对象_Php_Symfony_Doctrine_Dql - Fatal编程技术网

Php 使用DQL在Symfony2/doctor中选择具有子对象的对象

Php 使用DQL在Symfony2/doctor中选择具有子对象的对象,php,symfony,doctrine,dql,Php,Symfony,Doctrine,Dql,我有一篇文章和子类别实体,我想使用Doctrine DQL选择所有包含1篇或多篇文章的子类别,我不想选择空的子类别。。如何在一个查询中做到这一点 <?php namespace Evr\HomeBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * Subcategory * * @ORM\Table(name="ev_subca

我有一篇文章和子类别实体,我想使用Doctrine DQL选择所有包含1篇或多篇文章的子类别,我不想选择空的子类别。。如何在一个查询中做到这一点

<?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;
    }
}
以下是我的物品:

文章

<?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();
    }

}

通过QueryBuilder,查询实体“子类别”,并使用方法
->join
,将其连接到实体“文章”,就这样。非常感谢