我试图从几个表中检索数据来制作一个json,但我仍然坚持在2个键上使用UniqueConstraint的表。
这是我的QueryBuilder:
$qb = $this->_em->createQueryBuilder()
->select('partial s.{id, activity}, partial a.{id, title}, partial p.{id, evaluationType}')
->from('Innova\PathBundle\Entity\Step', 's')
->leftJoin('s.activity', 'a') //join on Activity entity
->leftJoin('a.parameters', 'p') // join on ActivityParameters entity
->andWhere('s.path = 2')
;
但我想加入评估实体,即:
/**
* @ORM\Table(
* name="claro_activity_evaluation",
* uniqueConstraints={
* @ORM\UniqueConstraint(
* name="user_activity_unique_evaluation",
* columns={"user_id", "activity_parameters_id"}
* )
* }
* )
*/
class Evaluation
{
/**
* @ORM\ManyToOne(targetEntity="Claroline\CoreBundle\Entity\User")
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Claroline\CoreBundle\Entity\Activity\ActivityParameters")
* @ORM\JoinColumn(name="activity_parameters_id", onDelete="CASCADE", nullable=false)
*/
protected $activityParameters;
/**
* @ORM\Column(name="attempts_count", type="integer", nullable=true)
*/
protected $attemptsCount;
}
用户实体:
/**
* @ORM\Table(name="claro_user")
* @ORM\Entity(repositoryClass="Claroline\CoreBundle\Repository\UserRepository")
*/
class User
{
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="first_name", length=50)
* @Assert\NotBlank()
*/
protected $firstName;
}
ActivityParameters实体
/**
* @ORM\Entity
* @ORM\Table(name="claro_activity_parameters")
*/
class ActivityParameters
{
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var \Claroline\CoreBundle\Entity\Resource\Activity
*
* @ORM\OneToOne(
* targetEntity="Claroline\CoreBundle\Entity\Resource\Activity",
* mappedBy="parameters"
* )
* @ORM\JoinColumn(name="activity_id", onDelete="CASCADE", nullable=true)
*/
protected $activity;
/**
* @var string
*
* @ORM\Column(name="evaluation_type", nullable=true)
*/
protected $evaluationType;
/**
* @return string
*/
public function getEvaluationType()
{
return $this->evaluationType;
}
}
活动实体
/**
* @ORM\Table(name="claro_activity")
*/
class Activity
{
/**
* @var string
* @ORM\Column(length=255, nullable=true)
*/
protected $title;
/**
* @ORM\OneToOne(
* targetEntity="Claroline\CoreBundle\Entity\Activity\ActivityParameters",
* inversedBy="activity",
* cascade={"persist"}
* )
* @ORM\JoinColumn(name="parameters_id", onDelete="cascade", nullable=true)
*/
protected $parameters;
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
我不知道如何修改此查询构建器以从评估实体中检索hte数据。我想要这样的东西:
$qb = $this->_em->createQueryBuilder()
->select('partial s.{id, activity}, partial a.{id, title}, partial p.{id, evaluationType}, e')
->from('Innova\PathBundle\Entity\Step', 's')
->leftJoin('s.activity', 'a') //join on Activity entity
->leftJoin('a.parameters', 'p') // join on ActivityParameters entity
->andWhere('s.path = 2')
->leftJoin('?i dont know what?', 'e') // join on Evaluation entity
->andWhere('e.user = 3') //data for a specific user
;
感谢您的帮助
答案 0 :(得分:0)
我"似乎"找到一个近乎工作的解决方案,阅读this topic:我必须做一个子查询:
$qb->select('e, partial p.{id, evaluationType}, partial a.{id, title}')
->from('Claroline\CoreBundle\Entity\Activity\Evaluation', 'e')
->leftJoin('e.activityParameters', 'p')
->leftJoin('p.activity', 'a')
->where(
$qb->expr()->in( //needs a subquery
'a.id',
$subq->select('a2.id')
->from('Innova\PathBundle\Entity\Step', 's')
->leftJoin(
's.activity',
'a2',
\Doctrine\ORM\Query\Expr\Join::WITH,
$subq->expr()->eq('s.path', '?1') //can't do a andWhere('s.path = :path'))
)
->getDQL() //needs a dql, not a querybuilder for the main query
)
)
->andWhere($qb->expr()->eq('e.user', '?2')) //can't do a andWhere('e.user = :user'))
->setParameter(1, $pid)
->setParameter(2, $uid)
;
我还想在查询结果中检索Step实体数据(id,...)。我无法添加'主要选择:我有Error: 's' is used outside the scope of its declaration.