Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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 查询生成器-获取另一个查询旁边的行数_Php_Mysql_Doctrine_Symfony_Query Builder - Fatal编程技术网

Php 查询生成器-获取另一个查询旁边的行数

Php 查询生成器-获取另一个查询旁边的行数,php,mysql,doctrine,symfony,query-builder,Php,Mysql,Doctrine,Symfony,Query Builder,我正在参加一个摄影比赛,我有这样的关系 所有图像都有和id、标题、作者和文件名 namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Image { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") *

我正在参加一个摄影比赛,我有这样的关系

  • 所有图像都有和id、标题、作者和文件名
  • namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     */
    class Image {
    
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
        /**
         * @ORM\Column(type="string", length=255, name="file_name")
         * @var string $fileName
         */
        protected $fileName;
    
        /**
         * @ORM\Column(type="string", length=255, name="title", nullable=true)
         * @var string $title
         */
        protected $title = null;
    
        /**
         * @ORM\Column(type="string", length=255, name="author", nullable=true)
         * @var string $author
         */
        protected $author = null;
    
        /**
         * @ORM\Column(type="datetime", name="upload_at")
         */
        protected $uploadDate;
    
        /**
         * @ORM\Column(type="boolean", name="in_pool")
         */
        protected $inPool;
    
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set fileName
         *
         * @param string $fileName
         *
         * @return Image
         */
        public function setFileName($fileName)
        {
            $this->fileName = $fileName;
    
            return $this;
        }
    
        /**
         * Get fileName
         *
         * @return string
         */
        public function getFileName()
        {
            return $this->fileName;
        }
    
        /**
         * Set title
         *
         * @param string $title
         *
         * @return Image
         */
        public function setTitle($title)
        {
            $this->title = $title;
    
            return $this;
        }
    
        /**
         * Get title
         *
         * @return string
         */
        public function getTitle()
        {
            return $this->title;
        }
    
        /**
         * Set author
         *
         * @param string $author
         *
         * @return Image
         */
        public function setAuthor($author)
        {
            $this->author = $author;
    
            return $this;
        }
    
        /**
         * Get author
         *
         * @return string
         */
        public function getAuthor()
        {
            return $this->author;
        }
    
        /**
         * Set uploadDate
         *
         * @param \DateTime $uploadDate
         *
         * @return Image
         */
        public function setUploadDate($uploadDate)
        {
            $this->uploadDate = $uploadDate;
    
            return $this;
        }
    
        /**
         * Get uploadDate
         *
         * @return \DateTime
         */
        public function getUploadDate()
        {
            return $this->uploadDate;
        }
    
        /**
         * Set inPool
         *
         * @param boolean $inPool
         *
         * @return Image
         */
        public function setInPool($inPool)
        {
            $this->inPool = $inPool;
    
            return $this;
        }
    
        /**
         * Get inPool
         *
         * @return boolean
         */
        public function getInPool()
        {
            return $this->inPool;
        }
    
        public function __construct()
        {
            $this->uploadDate = new \DateTime('now');
            $this->inPool = false;
        }
    }
    
  • 所有投票都有一个id、投票图像的id和数据
  • namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     */
    class Image {
    
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
        /**
         * @ORM\Column(type="string", length=255, name="file_name")
         * @var string $fileName
         */
        protected $fileName;
    
        /**
         * @ORM\Column(type="string", length=255, name="title", nullable=true)
         * @var string $title
         */
        protected $title = null;
    
        /**
         * @ORM\Column(type="string", length=255, name="author", nullable=true)
         * @var string $author
         */
        protected $author = null;
    
        /**
         * @ORM\Column(type="datetime", name="upload_at")
         */
        protected $uploadDate;
    
        /**
         * @ORM\Column(type="boolean", name="in_pool")
         */
        protected $inPool;
    
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set fileName
         *
         * @param string $fileName
         *
         * @return Image
         */
        public function setFileName($fileName)
        {
            $this->fileName = $fileName;
    
            return $this;
        }
    
        /**
         * Get fileName
         *
         * @return string
         */
        public function getFileName()
        {
            return $this->fileName;
        }
    
        /**
         * Set title
         *
         * @param string $title
         *
         * @return Image
         */
        public function setTitle($title)
        {
            $this->title = $title;
    
            return $this;
        }
    
        /**
         * Get title
         *
         * @return string
         */
        public function getTitle()
        {
            return $this->title;
        }
    
        /**
         * Set author
         *
         * @param string $author
         *
         * @return Image
         */
        public function setAuthor($author)
        {
            $this->author = $author;
    
            return $this;
        }
    
        /**
         * Get author
         *
         * @return string
         */
        public function getAuthor()
        {
            return $this->author;
        }
    
        /**
         * Set uploadDate
         *
         * @param \DateTime $uploadDate
         *
         * @return Image
         */
        public function setUploadDate($uploadDate)
        {
            $this->uploadDate = $uploadDate;
    
            return $this;
        }
    
        /**
         * Get uploadDate
         *
         * @return \DateTime
         */
        public function getUploadDate()
        {
            return $this->uploadDate;
        }
    
        /**
         * Set inPool
         *
         * @param boolean $inPool
         *
         * @return Image
         */
        public function setInPool($inPool)
        {
            $this->inPool = $inPool;
    
            return $this;
        }
    
        /**
         * Get inPool
         *
         * @return boolean
         */
        public function getInPool()
        {
            return $this->inPool;
        }
    
        public function __construct()
        {
            $this->uploadDate = new \DateTime('now');
            $this->inPool = false;
        }
    }
    
以下是项目中的实体:

图像
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Image {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length=255, name="file_name")
     * @var string $fileName
     */
    protected $fileName;

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

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

    /**
     * @ORM\Column(type="datetime", name="upload_at")
     */
    protected $uploadDate;

    /**
     * @ORM\Column(type="boolean", name="in_pool")
     */
    protected $inPool;


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

    /**
     * Set fileName
     *
     * @param string $fileName
     *
     * @return Image
     */
    public function setFileName($fileName)
    {
        $this->fileName = $fileName;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set author
     *
     * @param string $author
     *
     * @return Image
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

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

    /**
     * Set uploadDate
     *
     * @param \DateTime $uploadDate
     *
     * @return Image
     */
    public function setUploadDate($uploadDate)
    {
        $this->uploadDate = $uploadDate;

        return $this;
    }

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

    /**
     * Set inPool
     *
     * @param boolean $inPool
     *
     * @return Image
     */
    public function setInPool($inPool)
    {
        $this->inPool = $inPool;

        return $this;
    }

    /**
     * Get inPool
     *
     * @return boolean
     */
    public function getInPool()
    {
        return $this->inPool;
    }

    public function __construct()
    {
        $this->uploadDate = new \DateTime('now');
        $this->inPool = false;
    }
}
投票

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Image {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length=255, name="file_name")
     * @var string $fileName
     */
    protected $fileName;

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

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

    /**
     * @ORM\Column(type="datetime", name="upload_at")
     */
    protected $uploadDate;

    /**
     * @ORM\Column(type="boolean", name="in_pool")
     */
    protected $inPool;


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

    /**
     * Set fileName
     *
     * @param string $fileName
     *
     * @return Image
     */
    public function setFileName($fileName)
    {
        $this->fileName = $fileName;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set author
     *
     * @param string $author
     *
     * @return Image
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

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

    /**
     * Set uploadDate
     *
     * @param \DateTime $uploadDate
     *
     * @return Image
     */
    public function setUploadDate($uploadDate)
    {
        $this->uploadDate = $uploadDate;

        return $this;
    }

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

    /**
     * Set inPool
     *
     * @param boolean $inPool
     *
     * @return Image
     */
    public function setInPool($inPool)
    {
        $this->inPool = $inPool;

        return $this;
    }

    /**
     * Get inPool
     *
     * @return boolean
     */
    public function getInPool()
    {
        return $this->inPool;
    }

    public function __construct()
    {
        $this->uploadDate = new \DateTime('now');
        $this->inPool = false;
    }
}

在投票中添加反向

/**
         * @ORM\ManyToOne(targetEntity="Image", inversedBy="votes")
         * @ORM\JoinColumn(onDelete="CASCADE")
         */
        private $image;
在图像实体中添加属性$vots

  /**
         * One image has many votes
         * @ORM\OneToMany(targetEntity="Vote", mappedBy="image")
         */
        private $votes;

  function __construct() {
       $this-> votes = new ArrayCollection()
  }
然后使用getter,您可以获得选票收集:

function getVotes()
{
   return $this->votes;
}

ArrayCollection有count()方法,因此您可以使用它来获取
投票
实体的存储库类中的票数(它应该是
VoteRepository
,如果它不存在,则创建该类)添加方法
countVotesForImage

/**
 * Method returns count of votes assigned to an Image entity
 *
 * @param Image $image Instance of Image entity
 *
 * @return int Result of count query
 */
public function countVotesForImage(Image $image)
{
    $imageID = $image->getId();

    if (!is_numeric($imageID)) {
        // for new images which are not stored in DB
        // we dont have to query database
        return 0;
    }

    return (int) $this->createQueryBuilder('v')
        ->select('COUNT(v.id)')
        ->where('v.image = :id')
        ->setParameter('id', $image->getId())
        ->getQuery()
        ->getSingleScalarResult();
}
进入任何控制器后,您将能够使用它:

$image; // this variable should contain valid class of Image entity
$counter = $this->getDoctrine()->getRepository('AppBundle:Vote')
    ->countVotesForImage($image);
最后一步是将其传递给视图:

return $this->render(
    'your/template.html.twig',
    array('counter' => $counter)
);
如果要对所有图像进行所有投票,请将此方法添加到
ImageRepository

/**
 * Method returns count of votes assigned to an Image entity
 *
 * @return ArrayCollection
 */
public function countVotesForAllImages()
{
    return $this->createQueryBuilder('i')
        ->select(array(
            'i',
            'COUNT(i.id) as counter'
        ))
        ->join('i.votes', 'v')
        ->groupBy('i.id')
        ->getQuery()
        ->getResult();
}
如果添加此方法,还需要添加投票实体:

/**
 * This is internal variable, it does not have to be mapped into database field
 * @var int Counter of votes for an Image
 */
private $counter;

/**
 * Method sets internal counter of votes for an Image
 *
 * @param int $counter
 */
public function setCounter($counter)
{
    $this->counter = $counter;
}

/**
 * Method gets internal counter of votes for an Image
 *
 * @return int
 */
public function getCounter()
{
    return $this->counter;
}

@Markownikow提出的方法也会起作用,但它会将DB中的所有投票放入内存中,将它们水合,然后在内存中作为数组项进行计数。因为这太过分了,我建议选择我的方法。

使用了您的代码。我的选择如下:`$images=$em->createQueryBuilder()->select('i,count(i.voces))->from('AppBundle:Image','i')->orderBy('id','DESC')->getQuery()->getResult();`但是我得到了一个无效的表达式。StateFieldPathExpression或`
SingleValuedAssociationField应为。
此方法将DB中的所有投票放入内存,将其水合物化,然后在内存中作为数组项计数。因为这可能会有点过分,所以我建议选择我的方法(见我的答案)。我认为michail_w有更好的方法,实际上我也想提出一些类似于他在countVotesForAllImages()方法中所做的事情,但我对正确的语法没有信心,如果关于您的错误我不太确定,但可能是因为它应该是“->orderBy('I.id','DESC'))-“i”别名丢失