gpt4 book ai didi

php - symfony2 Doctrine ResultSetMapping 错误

转载 作者:可可西里 更新时间:2023-10-31 23:22:48 30 4
gpt4 key购买 nike

这里我尝试使用ResultSetMapping 来映射nativeQuery 生成的结果。但是我不知道为什么该 Doctrine 总是抛出 Undefined index 错误。谁能帮我解决这个问题?这是我的代码:PlaceMarkerRepository:

namespace Leo\FoodRadarBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMappingBuilder;


/**
* PlaceMarkerRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PlaceMarkerRepository extends EntityRepository
{
public function findNearBy($query,$lat, $lng, $radius){

$rsm = new ResultSetMappingBuilder($this->getEntityManager());
// $rsm = new ResultSetMappingBuilder();
// $rsm->addRootEntityFromClassMetadata('LeoFoodRadarBundle:PlaceMarker', 'p');
// $rsm->addJoinedEntityFromClassMetadata('LeoFoodRadarBundle:Coordinate', 'c', 'p', 'coordinate', array('id' => 'coordinate_id'));

$rsm->addEntityResult('LeoFoodRadarBundle:PlaceMarker', 'p');
$rsm->addFieldResult('p', 'id', 'id');
$rsm->addFieldResult('p', 'name', 'name');
$rsm->addJoinedEntityResult('LeoFoodRadarBundle:Coordinate' , 'c', 'p', 'coordinate');
$rsm->addFieldResult('c', 'coordinate_id', 'id');
$rsm->addFieldResult('c', 'latitude', 'latitude');
$rsm->addFieldResult('c', 'longitude', 'longitude');



$sql ="SELECT p.id, p.name, c.latitude, c.longitude, c.id as coordinate_id
FROM placeMarker p join coordinate c on p.coordinate_id = c.id
";

$this->getEntityManager()->createNativeQuery($sql, $rsm)->getResult();



}
}

和 PlaceMark:

namespace Leo\FoodRadarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Leo\FoodRadarBundle\Entity\PlaceMarker
*
* @ORM\Table(name="placeMarker")
* @ORM\Entity(repositoryClass="Leo\FoodRadarBundle\Entity\PlaceMarkerRepository")
*/
class PlaceMarker {

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

/**
* @var Coordinate coordinate
* @ORM\OneToOne(targetEntity="Coordinate")
* @ORM\JoinColumn(name="coordinate_id", referencedColumnName="id")
*/
private $coordinate;

/**
*
* @var Address address
* @ORM\OneToOne(targetEntity="Address")
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
*/
private $address;


/**
*
* @ORM\Column(name="name", type="string", length=100)
*/
private $name;

/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}

/**
* Set coordinate
*
* @param Leo\FoodRadarBundle\Entity\Coordinate $coordinate
* @return PlaceMarker
*/
public function setCoordinate(\Leo\FoodRadarBundle\Entity\Coordinate $coordinate = null) {
$this->coordinate = $coordinate;

return $this;
}

/**
* Get coordinate
*
* @return Leo\FoodRadarBundle\Entity\Coordinate
*/
public function getCoordinate() {
return $this->coordinate;
}

/**
* Set address
*
* @param Leo\FoodRadarBundle\Entity\Address $address
* @return PlaceMarker
*/
public function setAddress(\Leo\FoodRadarBundle\Entity\Address $address = null) {
$this->address = $address;

return $this;
}

/**
* Get address
*
* @return Leo\FoodRadarBundle\Entity\Address
*/
public function getAddress() {
return $this->address;
}



/**
* Set name
*
* @param string $name
* @return PlaceMarker
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}

坐标:

<?php

namespace Leo\FoodRadarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Leo\FoodRadarBundle\Entity\Coordinate
*
* @ORM\Table(name="coordinate")
* @ORM\Entity
*/
class Coordinate
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\Column(type="float")
*/
private $longitude;

/**
* @ORM\Column(type="float")
*/
private $latitude;

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}


/**
* Get longitude
* @return float
*/
public function getLongitude() {
return $this->longitude;
}

/**
* Set longitude
*/
public function setLongitude($longitude) {
$this->longitude = $longitude;
}

/**
* Get Latitude
* @return float
*/
public function getLatitude() {
return $this->latitude;
}

/**
* Set Latitude
*/
public function setLatitude($latitude) {
$this->latitude = $latitude;
}


}

该语句引起的错误:

$targetClass = $this->_ce[$relation['targetEntity']];

错误信息为:

Notice: Undefined index: Leo\FoodRadarBundle\Entity\Coordinate in /Volumes/DATA/LEO/Sites/FoodRadar/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 427 

ErrorHandler ->handle ('8', 'Undefined index: Leo\FoodRadarBundle\Entity\Coordinate', '/Volumes/DATA/LEO/Sites/FoodRadar/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php', '427', array('row' => array('id' => '1', 'name' => 'test', 'latitude' => '233.234342', 'longitude' => '-232.23232323232', 'coordinate_id' => '2'), 'cache' => array('id' => array('fieldName' => 'id', 'type' => object(IntegerType), 'isIdentifier' => true, 'dqlAlias' => 'p'), 'name' => array('fieldName' => 'name', 'type' => object(StringType), 'isIdentifier' => false, 'dqlAlias' => 'p'), 'latitude' => array('fieldName' => 'latitude', 'type' => object(FloatType), 'isIdentifier' => false, 'dqlAlias' => 'c'), 'longitude' => array('fieldName' => 'longitude', 'type' => object(FloatType), 'isIdentifier' => false, 'dqlAlias' => 'c'), 'coordinate_id' => array('fieldName' => 'id', 'type' => object(IntegerType), 'isIdentifier' => true, 'dqlAlias' => 'c')), 'result' => array(object(PlaceMarker)), 'id' => array('p' => '|1', 'c' => '|2'), 'nonemptyComponents' => array('p' => true, 'c' => true), 'rowData' => array('p' => array('id' => '1', 'name' => 'test'), 'c' => array('latitude' => '233.234342', 'longitude' => '-232.23232323232', 'id' => '2')), 'dqlAlias' => 'c', 'data' => array('latitude' => '233.234342', 'longitude' => '-232.23232323232', 'id' => '2'), 'entityName' => 'LeoFoodRadarBundle:Coordinate', 'parentAlias' => 'p', 'path' => 'p.c', 'parentObject' => object(PlaceMarker), 'parentClass' => object(ClassMetadata), 'oid' => '000000003e34db9d00000000492221b6', 'relationField' => 'coordinate', 'relation' => array('fieldName' => 'coordinate', 'targetEntity' => 'Leo\FoodRadarBundle\Entity\Coordinate', 'joinColumns' => array(array('name' => 'coordinate_id', 'unique' => true, 'nullable' => true, 'onDelete' => null, 'columnDefinition' => null, 'referencedColumnName' => 'id')), 'mappedBy' => null, 'inversedBy' => null, 'cascade' => array(), 'orphanRemoval' => false, 'fetch' => '2', 'type' => '1', 'isOwningSide' => true, 'sourceEntity' => 'Leo\FoodRadarBundle\Entity\PlaceMarker', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false, 'sourceToTargetKeyColumns' => array(*DEEP NESTED ARRAY*), 'joinColumnFieldNames' => array(*DEEP NESTED ARRAY*), 'targetToSourceKeyColumns' => array(*DEEP NESTED ARRAY*)), 'reflField' => object(ReflectionProperty), 'reflFieldValue' => null, 'element' => object(Coordinate), 'entityKey' => '0', 'resultKey' => '0'))
in /Volumes/DATA/LEO/Sites/FoodRadar/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php at line 427 -+

最佳答案

终于找到导致这个问题的原因了。我认为这是因为 resultsetmapping 在内部运行,所以它无法理解 Symfony 提供的逻辑名称。这个问题的解决方案是将逻辑名更改为完整路径名。然后它将起作用。

关于php - symfony2 Doctrine ResultSetMapping 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12950497/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com