我有以下代码:
$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,两者都具有相同的参考。但我尝试改变它,它仍然给我警告。
答案 0 :(得分: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作为参数传递,该参数是actors表中Presentation的外键字段。请注意,我不会为演示文稿请求信息,但是,Doctrine会对对象进行水合,我可以通过这种方式从结果的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();
因此,您可以获得图片,通过这些,您可以获得所需的商店信息。
答案 1 :(得分: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 set
NOT table
shop.id AS shop_id
使用$rsm->addFieldResult('s', 'shop_id', 'id');