我想在理论上实现一个非常简单的查询,但我没有设法让它工作: 我想要按Elo分组的活动简历数量(这是继承表中的属性)。
错误:
[Semantical Error] line 0, col 22 near 'elo FROM MyNamespace\CvBundle\Entity\Cv'
: Error: Class MyNamespace\CvBundle\Entity\Cv\Feature has no field or association named elo.
它抱怨MyNamespace\CvBundle\Entity\Cv\Feature
中没有字段,因为它是“主”表。此字段包含在MyNamespace\CvBundle\Entity\Cv\Lol
中,这是一个继承自Cv\Feature
这是查询:
// CvRepository.php
public function getStats()
{
$query = $this->createQueryBuilder('c')
->select('COUNT(f.id), f.elo')
->leftJoin('c.feature', 'f')
->groupBy('f.elo')
->where('f INSTANCE OF MyNameSpace\CvBundle\Entity\Cv\Lol')
->andWhere('c.active = :active')
->andWhere('c.expiresAt > :now')
->setParameters(array(
'now' => new \DateTime("now"),
'active' => 1,
))
->getQuery();
return $query->execute();
}
表格Cv:
// Cv.php
/**
* @ORM\Table(name="cv")
* @ORM\Entity(...)
*/
class Cv
{
/**
* @ORM\OneToOne(targetEntity="MyNameSpace\CvBundle\Entity\Cv\Feature", cascade={"all"})
*/
protected $feature;
}
Feature.php
/**
* @ORM\Entity()
* @ORM\Table(name="cv_feature")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"lol" = "Lol", ...})
*/
abstract class Feature
{
/**
* @ORM\OneToOne(targetEntity="MyNameSpace\CvBundle\Entity\Cv")
* @ORM\JoinColumn(name="cv_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $cv;
和Lol.php
/**
* @ORM\Entity()
*/
class Lol extends Feature
{
/**
* @var integer $elo
*
* @ORM\Column(name="elo", type="string")
*/
private $elo;
....
感谢您的帮助
答案 0 :(得分:1)
非常确定你必须将$ elo移动到你的Feature类。
你的'where instance of'会将结果限制为Lol类,但是我怀疑DQl是否足够聪明,以至于所有功能都会变成大声笑。
你可能会改变Cv指向Lol,但这可能不是你想要的。
您也可以在php中实现该组。
但试试这个并验证它有效:
abstract class Feature
{
/**
* @ORM\OneToOne(targetEntity="MyNameSpace\CvBundle\Entity\Cv")
* @ORM\JoinColumn(name="cv_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $cv;
/**
* @var integer $elo
*
* @ORM\Column(name="elo", type="string")
*/
protected $elo;
你只会在你的Lol课程中为elo安装getter / setter。所以它基本上隐藏了它的兄弟姐妹。无论如何它已经存在于数据库表中。您甚至可以将其保密为私有并将其添加到Lol中,因此兄弟姐妹根本无法访问它。不确定,但我认为学说可能仍然保持它。