我有两个实体,如Country
和Region
。以下是实体:
Country
Entity:
<?php
namespace Catalog'Models'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* A country page.
*
* @ORM'Entity
* @ORM'Table(name="tbl_country")
* @property string $name
* @property string $code
* @property string $latitude
* @property string $longitude
* @property string $countryLogo
* @property int $id
*/
class Country {
/**
* @ORM'Id
* @ORM'Column(type="integer");
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string",length=250,nullable=true)
*/
private $name;
/**
* @ORM'Column(type="string",length=5,nullable=true)
*/
private $code;
/**
* @ORM'Column(type="float",nullable=true)
*/
private $latitude;
/**
* @ORM'Column(type="float",nullable=true)
*/
private $longitude;
/**
* @ORM'Column(type="string",length=120,nullable=true)
*/
private $countryLogo;
/**
* @ORM'OneToMany(targetEntity="Catalog'Models'Entity'Region", mappedBy="country")
*/
protected $regions;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getCode() {
return $this->code;
}
public function setCode($code) {
$this->code = $code;
}
public function getLatitude() {
return $this->latitude;
}
public function setLatitude($latitude) {
$this->latitude = $latitude;
}
public function getLongitude() {
return $this->longitude;
}
public function setLongitude($longitude) {
$this->longitude = $longitude;
}
public function getCountryLogo() {
return $this->countryLogo;
}
public function setCountryLogo($countryLogo) {
$this->countryLogo = $countryLogo;
}
public function getRegions() {
return $this->regions;
}
public function setRegions($regions) {
$this->regions = $regions;
}
}
Region
Entity:
<?php
namespace Catalog'Models'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* A region page.
*
* @ORM'Entity
* @ORM'Table(name="tbl_region")
* @property string $name
* @property string $code
* @property string $latitude
* @property string $longitude
* @property int $id
*/
class Region {
/**
* @ORM'Id
* @ORM'Column(type="integer");
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string",length=250,nullable=true)
*/
private $name;
/**
* @ORM'Column(type="string",length=5,nullable=true)
*/
private $code;
/**
* @ORM'Column(type="float",nullable=true)
*/
private $latitude;
/**
* @ORM'Column(type="float",nullable=true)
*/
private $longitude;
/**
* @ORM'ManyToOne(targetEntity="Catalog'Models'Entity'Country", inversedBy="regions")
* @ORM'JoinColumn(name="country_id", referencedColumnName="id")
*/
private $country;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getCode() {
return $this->code;
}
public function setCode($code) {
$this->code = $code;
}
public function getLatitude() {
return $this->latitude;
}
public function setLatitude($latitude) {
$this->latitude = $latitude;
}
public function getLongitude() {
return $this->longitude;
}
public function setLongitude($longitude) {
$this->longitude = $longitude;
}
public function getCountry() {
return $this->country;
}
public function setCountry($country) {
$this->country = $country;
}
}
我正试图得到所有的国家。但是当我这样做的时候
$countries = $this->getEntityManager()->getRepository('Catalog'Models'Entity'Country')->findAll();
它正在返回所有的国家和地区。
当我尝试这个
$countries = $this->getEntityManager()->createQuery("SELECT Country.id, Country.name FROM Catalog'Models'Entity'Country Country")->execute();
它只返回数组格式的Country
。我怎样才能得到对象格式的Country
?
您可以尝试将区域关联标记为延迟加载:
class Country {
...
/**
* @ORM'OneToMany(targetEntity="Catalog'Models'Entity'Region", mappedBy="country", fetch="EXTRA_LAZY")
*/
protected $regions;
查看文档:https://doctrine-orm.readthedocs.org/en/latest/tutorials/extra-lazy-associations.html?highlight=fetch
第一个方法应该可以工作,但是如果你想让你的查询工作,你应该这样做:
$query = $this->getEntityManager()->createQuery(
"SELECT c FROM Catalog'Models'Entity'Country c"
);
$countries = $query->getResult();
问题出在你的select语句中。如果你想在你的结果中有一个对象(实体)的集合,你应该只通过它的别名来查询实体,而不是在SELECT
语句中指定属性。如果你这样做,它将只返回你所要求的数组格式的属性。