0

我有 2 个具有多对一单向关系的简单表:

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.....
4

1 回答 1

0

您可以使用 Doctrine Query builder 来完成这项工作。假设您有 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();
于 2013-11-21T05:43:51.427 回答