关联实体结果问题:未定义索引:Doctrine/ORM/UnitOfWork.php中的id

关联实体结果问题:未定义索引:Doctrine/ORM/UnitOfWork.php中的id,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我有以下代码: $rsm = new ResultSetMapping(); $rsm->addEntityResult('App\MainBundle\Entity\InstagramShopPicture', 'p'); $rsm->addFieldResult('p', 'id', 'id'); $rsm->addFieldResult('p','lowresimageurl','lowresimageurl

我有以下代码:

        $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,两者具有相同的参考。但是我试着改变它,它仍然给我警告。

问题在于

$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结果集中的列的名称
而不是

shop.id AS shop_id
使用
$rsm->addFieldResult('s','shop_id','id')

问题出在

$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结果集中的列的名称
而不是

shop.id AS shop_id

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

几周前我遇到了一个类似的问题,我可以按如下方式解决它:

    $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()函数,该函数用于外键或鉴别器列。 您可以在官方条令文件的第17.3.5章中了解这一点。 在这个函数中,我将presentation_id作为参数传递,它是actors表中presentation外键的字段。请注意,我不要求提供演示文稿的信息,但是,我可以通过以下方式从结果的参与者实体访问演示文稿的所有信息:

    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();

因此,你可以得到图片,通过这些图片,你可以得到你需要的商店的信息。

几周前我遇到了一个类似的问题,我可以如下解决:

    $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()函数,该函数用于外键或鉴别器列。 您可以在官方条令文件的第17.3.5章中了解这一点。 在这个函数中,我将presentation_id作为参数传递,它是actors表中presentation外键的字段。请注意,我不要求提供演示文稿的信息,但是,我可以通过以下方式从结果的参与者实体访问演示文稿的所有信息:

    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();

因此,您可以获得图片,通过这些图片,您可以获得所需的店铺信息。

$rsm->addFieldResult('p','id','id')
它指向这一行,对吗?@Viscocent我不知道,当我删除addJoinedEntityResult和它下面的两行时,它工作正常,我认为错误与$rsm->addFieldResult('s','id','id')有关;在您的
InstagramShop
表中,您真的有字段id吗?@Viscocent我在上面为您添加了我的实体see@adit如果你认为我的答案对你有效,我会很感激,因为如果你不给予赏金,那么只有一半的赏金会被分配。非常感谢。
$rsm->addFieldResult('p','id','id')
它指向这一行,对吗?@Viscocent我不知道,当我删除addJoinedEntityResult和它下面的两行时,它工作正常,我认为错误与$rsm->addFieldResult('s','id','id')有关;在您的
InstagramShop
表中,您真的有字段id吗?@Viscocent我在上面为您添加了我的实体see@adit如果你认为我的答案对你有效,我会很感激,因为如果你不给予赏金,那么只有一半的赏金会被分配。非常感谢。我试着改变了,错误消失了,但是现在我无法找到商店购买该商品。执行项目->getShop()返回空值。我已尝试将shop.username添加到选择查询中。我尝试更改该选项,错误消失,但现在我无法获取该项目的店铺。执行项目->getShop()返回空值。我已尝试将shop.username添加到select查询中