2014-05-20 39 views
1

我有以下代碼:問題與加盟實體結果:未定義指數:ID在學說/ ORM/UnitOfWork.php

 $rsm = new ResultSetMapping(); 
     $rsm->addEntityResult('App\MainBundle\Entity\InstagramShopPicture', 'p'); 
     $rsm->addFieldResult('p', 'id', 'id'); 
     $rsm->addFieldResult('p','lowresimageurl','lowresimageurl'); 
     $rsm->addFieldResult('p','medresimageurl','medresimageurl'); 
     $rsm->addFieldResult('p','highresimageurl','highresimageurl'); 
     $rsm->addFieldResult('p','caption','caption'); 
     $rsm->addFieldResult('p','numberoflikes','numberoflikes'); 
     $rsm->addFieldResult('p','numberofdislikes','numberofdislikes'); 
     $rsm->addJoinedEntityResult('App\MainBundle\Entity\InstagramShop', 's', 'p', 'shop'); 
     $rsm->addFieldResult('s', 'id', 'id'); 
     $rsm->addFieldResult('s', 'username', 'username'); 

     $query = $em->createNativeQuery('SELECT picture.id, picture.lowresimageurl, picture.medresimageurl, picture.highresimageurl, picture.caption, picture.numberoflikes, picture.numberofdislikes, shop.id AS shop_id , shop.username 
             FROM App_instagram_picture_category category 
             INNER JOIN App_instagram_shop_picture picture ON category.picture_id = picture.id 
             INNER JOIN App_instagram_shop shop ON shop.id = picture.shop_id 
             WHERE category.first_level_category_id = ? 
             AND picture.deletedAt IS NULL 
             AND shop.deletedAt IS NULL 
             AND shop.isLocked = 0 
             AND shop.expirydate IS NOT NULL 
             AND shop.expirydate > ? 
             AND shop.owner_id IS NOT NULL 
             GROUP BY shop.id 
             LIMIT ?' 

             , $rsm); 

     $query->setParameter(1, 10); 
     $query->setParameter(2, '2014-05-20'); 
     $query->setParameter(3, 10); 
     $itemsFromDifferentShops = $query->getResult(); 

但是我經常收到以下錯誤/警告:

Notice: Undefined index: id in /Users/Alex/Sites/App/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 2433 

這裏是我的實體是什麼樣子:

class InstagramShop 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * 
    * @var string 
    * @ORM\Column(name="username", type="string", nullable=true) 
    */ 
    private $username; 


    /** 
    * @Exclude() 
    * @ORM\OneToMany(targetEntity="InstagramShopPicture", mappedBy="shop", cascade= 
    {"persist"}) 
    * @ORM\OrderBy({"createdtimestamp" = "DESC"}) 
    */ 
    protected $userPictures; 

} 

class InstagramShopPicture 
{ 

     /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @Exclude() 
    * @ORM\ManyToOne(targetEntity="InstagramShop", inversedBy="userPictures") 
    * @ORM\JoinColumn(name="shop_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") 
    */ 
    protected $shop; 
} 

這是爲什麼?我如何解決它?我的懷疑是因爲有兩個ID。一個是產品ID,另一個是商店ID,兩者都有相同的參考。但我試圖改變它,它仍然給我警告。

+0

'$ rsm-> addFieldResult('p','id','id');'它指着這條線坐嗎? – Viscocent

+0

@Viscocent我不知道,當我刪除addJoinedEntityResult和它下面的兩行時,它工作正常,我認爲錯誤與$ rsm-> addFieldResult('s','id','id')有關; – adit

+0

在你的「InstagramShop」表中,你真的有一個字段ID? – Viscocent

回答

5

我有一個類似的問題,幾個星期前,我可以如下解決這個問題:

$em = $this->getEntityManager(); 

    $rsm = new \Doctrine\ORM\Query\ResultSetMapping(); 
    $rsm->addEntityResult('MyBundle:Actor', 'a'); 
    $rsm->addFieldResult('a', 'id', 'id'); 
    $rsm->addFieldResult('a', 'name', 'name'); 
    $rsm->addFieldResult('a', 'surname', 'surname'); 
    $rsm->addMetaResult('a', 'presentation_id', 'presentation_id'); 

    $query = $em->createNativeQuery(
      'SELECT a.id, a.name, a.surname, a.presentation_id 
      FROM actors AS a 
      INNER JOIN presentations AS p 
      WHERE p.id = a.presentation_id 
      AND p.finished = 0 
      AND p.id IN (?)', $rsm); 
    $query->setParameter(1, $presentations); 

    $actors = $query->getResult(); 

哪裏有你的代碼中的一些分歧,但也許你會爲了適應它來得到你想要的。

請注意用於外鍵或鑑別器列的addMetaResult()函數。 您可以在官方Doctrine文檔http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html的第17.3.5章中閱讀。 在這個函數中,我傳遞了presentation_id作爲參數,這是actor表中演示文件外鍵的字段。請注意,我不爲發言請求的信息,但是,原則滋潤對象,我可以從結果這樣的演員實體訪問演示文稿的所有信息:

foreach($actors as $a){ 
     // 
     // Some code here 
     // 

     $actorId  = $a->getId(); 
     $actorName  = $a->getName(); 
     $actorSurname = $a->getSurname(); 
     $preTitle  = $a->getPresentation()->getTitle(); 
     $preDesc  = $a->getPresentation()->getDescription(); 
     $preDirector = $a->getPresentation()->getDirector()->getFullName(); 

     // 
     // Some code here 
     // 
    } 

我認爲,你也可以用這種方式解決你的問題。也許,你可以這樣做:

$rsm = new ResultSetMapping(); 
    $rsm->addEntityResult('App\MainBundle\Entity\InstagramShopPicture', 'p'); 
    $rsm->addFieldResult('p', 'id', 'id'); 
    $rsm->addFieldResult('p','lowresimageurl','lowresimageurl'); 
    $rsm->addFieldResult('p','medresimageurl','medresimageurl'); 
    $rsm->addFieldResult('p','highresimageurl','highresimageurl'); 
    $rsm->addFieldResult('p','caption','caption'); 
    $rsm->addFieldResult('p','numberoflikes','numberoflikes'); 
    $rsm->addFieldResult('p','numberofdislikes','numberofdislikes'); 
    $rsm->addMetaResult('p', 'shop_id', 'shop_id'); 

    $query = $em->createNativeQuery('SELECT picture.id, picture.lowresimageurl, picture.medresimageurl, picture.highresimageurl, picture.caption, picture.numberoflikes, picture.numberofdislikes 
      FROM App_instagram_picture_category AS category 
      INNER JOIN App_instagram_shop_picture AS p ON category.picture_id = p.id 
      INNER JOIN App_instagram_shop AS shop ON shop.id = p.shop_id 
      WHERE category.first_level_category_id = ? 
      AND p.deletedAt IS NULL 
      AND shop.deletedAt IS NULL 
      AND shop.isLocked = 0 
      AND shop.expirydate IS NOT NULL 
      AND shop.expirydate > ? 
      AND shop.owner_id IS NOT NULL 
      GROUP BY shop.id 
      LIMIT ?' 

      , $rsm); 

    $query->setParameter(1, 10); 
    $query->setParameter(2, '2014-05-20'); 
    $query->setParameter(3, 10); 
    $itemsFromDifferentShops = $query->getResult(); 

因此,你會得到的圖片,並通過這些,你可以得到你需要的商店的信息。

2

問題是與

$rsm->addFieldResult('s', 'id', 'id'); 

根據定義

/** 
* Adds a field result that is part of an entity result or joined entity result. 
* 
* @param string $alias The alias of the entity result or joined entity result. 
* @param string $columnName The name of the column in the SQL result set. 
* @param string $fieldName The name of the field on the (joined) entity. 
*/ 
public function addFieldResult($alias, $columnName, $fieldName) 

第二個參數必須是SQL result settable

shop.id AS shop_id 

使用$rsm->addFieldResult('s', 'shop_id', 'id');

列名
+0

我試圖改變這種情況,錯誤消失了,但現在我無法獲得商品的商店。做一個item-> getShop()返回null。我已經嘗試在選擇查詢中添加shop.username – adit