我有两个简单的表,有多对一的单向关系:
class Event extends BaseEvent
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="Rposition")
* @ORM\JoinColumn(name="owner", referencedColumnName="id",onDelete="CASCADE", nullable=false )
**/
private $owner;
....
和所有者字段类的来源:
class Rposition
{
/**
* @var integer
*
* @ORM\Column(name="id", type="string", length=36)
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
*/
private $id;
/**
*
*
* @ORM\Column(name="scenario", type="string", length=255, nullable=false)
*/
private $scenario;
两个类都有正确的getter / setter和__tostring()
我需要实现这样的查询:
public function findAllByScenario($scenario) {
$q = $this->getEntityManager()->createQuery("SELECT e , r.scenario , r.id
FROM LapoMymeBundle:Event e
JOIN LapoMymeBundle:Rposition r
WITH e.owner = r.id
WHERE r.scenario = :scenario
ORDER BY e.start ASC, e.end ASC
")->setParameter('scenario', $scenario);
try {
return $q->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
- 它是一种正确的方式来加入并从中发回结果吗?
- 从对象数组中提取字段的正确方法是什么?
类似的东西:
$events=$this->getDoctrine()->getManager()
->getRepository('LapoMymeBundle:Event')->findAllByScenario($scenario);
$outputUsersArray = Array();
foreach($events as $event)
{
$eventArray = Array();
$eventArray[JSONResponseFields::KEY_ID] = $event->getId();
$eventArray[JSONResponseFields::KEY_NAME] = $event->getOwner()->getId();
$outputEventsArray[] = $eventArray;
} ....
出现错误喜欢:
FatalErrorException: Error: Call to a member function getId() on a non-object in.....
答案 0 :(得分:0)
您可以使用“学说查询”构建器来实现此功能。假设您有Querybuilder实例。查询看起来像
$ qb = this-> getDoctrine() - > getRepository('BundleName:Event') - > createQueryBuilder('e');
$query = $qb
->select('e','p')
->innerjoin('u.owner','r')
->where('r.scenario= :scenario')
->setParameter('scenario', $scenarioid)
->getQuery();
$chatuser = $query->getResult();