php – symfony2 doctrine ResultSetMapping错误
发布时间:2020-12-13 16:47:54 所属栏目:PHP教程 来源:网络整理
导读:在这里,我尝试使用ResultSetMapping映射由nativeQuery生成的结果.但我不知道为什么学说总是抛出未定义的索引错误.有人可以帮我解决这个问题吗? 这是我的代码: PlaceMarkerRepository: namespace LeoFoodRadarBundleEntity;use DoctrineORMEntityRepos
在这里,我尝试使用ResultSetMapping映射由nativeQuery生成的结果.但我不知道为什么学说总是抛出未定义的索引错误.有人可以帮我解决这个问题吗?
这是我的代码: PlaceMarkerRepository: namespace LeoFoodRadarBundleEntity; use DoctrineORMEntityRepository; use DoctrineORMQueryResultSetMappingBuilder; /** * 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','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 LeoFoodRadarBundleEntity; use DoctrineORMMapping as ORM; /** * LeoFoodRadarBundleEntityPlaceMarker * * @ORMTable(name="placeMarker") * @ORMEntity(repositoryClass="LeoFoodRadarBundleEntityPlaceMarkerRepository") */ class PlaceMarker { /** * @var integer $id * * @ORMColumn(name="id",type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ private $id; /** * @var Coordinate coordinate * @ORMOneToOne(targetEntity="Coordinate") * @ORMJoinColumn(name="coordinate_id",referencedColumnName="id") */ private $coordinate; /** * * @var Address address * @ORMOneToOne(targetEntity="Address") * @ORMJoinColumn(name="address_id",referencedColumnName="id") */ private $address; /** * * @ORMColumn(name="name",type="string",length=100) */ private $name; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set coordinate * * @param LeoFoodRadarBundleEntityCoordinate $coordinate * @return PlaceMarker */ public function setCoordinate(LeoFoodRadarBundleEntityCoordinate $coordinate = null) { $this->coordinate = $coordinate; return $this; } /** * Get coordinate * * @return LeoFoodRadarBundleEntityCoordinate */ public function getCoordinate() { return $this->coordinate; } /** * Set address * * @param LeoFoodRadarBundleEntityAddress $address * @return PlaceMarker */ public function setAddress(LeoFoodRadarBundleEntityAddress $address = null) { $this->address = $address; return $this; } /** * Get address * * @return LeoFoodRadarBundleEntityAddress */ 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 LeoFoodRadarBundleEntity; use DoctrineORMMapping as ORM; /** * LeoFoodRadarBundleEntityCoordinate * * @ORMTable(name="coordinate") * @ORMEntity */ class Coordinate { /** * @var integer $id * * @ORMColumn(name="id",type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ private $id; /** * @ORMColumn(type="float") */ private $longitude; /** * @ORMColumn(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: LeoFoodRadarBundleEntityCoordinate in /Volumes/DATA/LEO/Sites/FoodRadar/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 427 ErrorHandler ->handle ('8','Undefined index: LeoFoodRadarBundleEntityCoordinate','/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,'latitude' => array('fieldName' => 'latitude','type' => object(FloatType),'dqlAlias' => 'c'),'longitude' => array('fieldName' => 'longitude','coordinate_id' => array('fieldName' => 'id','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','id' => '2')),'dqlAlias' => 'c','data' => array('latitude' => '233.234342','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' => 'LeoFoodRadarBundleEntityCoordinate','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' => 'LeoFoodRadarBundleEntityPlaceMarker','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提供的逻辑名称.此问题的解决方案是将逻辑名称更改为完整路径名称.然后它会工作.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |