我有2个实体,例如Country
和Region
。以下是实体:
Country
实体:
<?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
实体:
<?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
?
答案 0 :(得分:2)
您可以尝试将区域分析标记为延迟加载:
class Country {
...
/**
* @ORM\OneToMany(targetEntity="Catalog\Models\Entity\Region", mappedBy="country", fetch="EXTRA_LAZY")
*/
protected $regions;
答案 1 :(得分:0)
第一种方法应该有效,但是如果你想让你的查询工作,你应该这样做:
$query = $this->getEntityManager()->createQuery(
"SELECT c FROM Catalog\Models\Entity\Country c"
);
$countries = $query->getResult();
问题出在你的select语句中。如果要在结果中使用对象(实体)的集合,则只应按其别名查询实体,而不在SELECT
语句中指定属性。如果这样做,它将仅返回您以数组格式要求的属性。