Symfony 2 教义左连接
Posted
技术标签:
【中文标题】Symfony 2 教义左连接【英文标题】:Symfony 2 doctrine left join 【发布时间】:2014-12-21 15:49:06 【问题描述】:我有疑问
$em = $this->getDoctrine()->getManager();
$query = $em->createQueryBuilder();
$query = $em->createQueryBuilder()
->select('com', 'cit')
->from('CatalogWebBundle:ComCompany', 'com')
->leftJoin('com.cmpCity', 'cit')
->getQuery()
->setMaxResults(1);
$info = $query->getResult();
工作正常,但我不知道如何从 JOINED TABLE 中获取结果。我尝试使用 JOINED 表 ENTITIES 中的 get 方法,但它不起作用?
也许我需要向 ENTITIES 做广告?
class ComCity
/**
* @var integer
*
* @ORM\Column(name="cit_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $citId;
/**
* @var string
*
* @ORM\Column(name="cit_name", type="string", length=255, nullable=true)
*/
private $citName;
/**
* Get citId
*
* @return integer
*/
public function getCitId()
return $this->citId;
/**
* Set citName
*
* @param string $citName
* @return ComCity
*/
public function setCitName($citName)
$this->citName = $citName;
return $this;
/**
* Get citName
*
* @return string
*/
public function getCitName()
return $this->citName;
命名空间目录\WebBundle\Entity;
使用 Doctrine\ORM\Mapping 作为 ORM;
class ComCompany
/**
* @var integer
*
* @ORM\Column(name="cmp_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $cmpId;
/**
* @var string
*
* @ORM\Column(name="cmp_name", type="string", length=255, nullable=true)
*/
private $cmpName;
/**
* @var integer
*
* @ORM\Column(name="cmp_code", type="bigint", nullable=true)
*/
private $cmpCode;
/**
* @var string
*
* @ORM\Column(name="cmp_vat", type="string", length=255, nullable=true)
*/
private $cmpVat;
/**
* @var integer
*
* @ORM\Column(name="cmp_emp", type="integer", nullable=true)
*/
private $cmpEmp;
/**
* @var string
*
* @ORM\Column(name="cmp_return", type="string", length=255, nullable=true)
*/
private $cmpReturn;
/**
* @var string
*
* @ORM\Column(name="cmp_return_from", type="string", length=255, nullable=true)
*/
private $cmpReturnFrom;
/**
* @var string
*
* @ORM\Column(name="cmp_return_till", type="string", length=255, nullable=true)
*/
private $cmpReturnTill;
/**
* @var string
*
* @ORM\Column(name="cmp_address", type="string", length=255, nullable=true)
*/
private $cmpAddress;
/**
* @var integer
*
* @ORM\Column(name="cmp_phone", type="bigint", nullable=true)
*/
private $cmpPhone;
/**
* @var string
*
* @ORM\Column(name="cmp_email", type="string", length=255, nullable=true)
*/
private $cmpEmail;
/**
* @var string
*
* @ORM\Column(name="cmp_site", type="string", length=255, nullable=true)
*/
private $cmpSite;
/**
* @var string
*
* @ORM\Column(name="cmp_ceo", type="string", length=255, nullable=true)
*/
private $cmpCeo;
/**
* @var string
*
* @ORM\Column(name="cmp_register", type="string", length=255, nullable=true)
*/
private $cmpRegister;
/**
* @var string
*
* @ORM\Column(name="cmp_url", type="string", length=255, nullable=false)
*/
private $cmpUrl;
/**
* @var \Catalog\WebBundle\Entity\ComCategory
*
* @ORM\ManyToOne(targetEntity="Catalog\WebBundle\Entity\ComCategory")
* @ORM\JoinColumns(
* @ORM\JoinColumn(name="cmp_category", referencedColumnName="cat_id")
* )
*/
private $cmpCategory;
/**
* @var \Catalog\WebBundle\Entity\ComCity
*
* @ORM\ManyToOne(targetEntity="Catalog\WebBundle\Entity\ComCity")
* @ORM\JoinColumns(
* @ORM\JoinColumn(name="cmp_city", referencedColumnName="cit_id")
* )
*/
private $cmpCity;
/**
* Get cmpId
*
* @return integer
*/
public function getCmpId()
return $this->cmpId;
/**
* Set cmpName
*
* @param string $cmpName
* @return ComCompany
*/
public function setCmpName($cmpName)
$this->cmpName = $cmpName;
return $this;
/**
* Get cmpName
*
* @return string
*/
public function getCmpName()
return $this->cmpName;
/**
* Set cmpCode
*
* @param integer $cmpCode
* @return ComCompany
*/
public function setCmpCode($cmpCode)
$this->cmpCode = $cmpCode;
return $this;
/**
* Get cmpCode
*
* @return integer
*/
public function getCmpCode()
return $this->cmpCode;
/**
* Set cmpVat
*
* @param string $cmpVat
* @return ComCompany
*/
public function setCmpVat($cmpVat)
$this->cmpVat = $cmpVat;
return $this;
/**
* Get cmpVat
*
* @return string
*/
public function getCmpVat()
return $this->cmpVat;
/**
* Set cmpEmp
*
* @param integer $cmpEmp
* @return ComCompany
*/
public function setCmpEmp($cmpEmp)
$this->cmpEmp = $cmpEmp;
return $this;
/**
* Get cmpEmp
*
* @return integer
*/
public function getCmpEmp()
return $this->cmpEmp;
/**
* Set cmpReturn
*
* @param string $cmpReturn
* @return ComCompany
*/
public function setCmpReturn($cmpReturn)
$this->cmpReturn = $cmpReturn;
return $this;
/**
* Get cmpReturn
*
* @return string
*/
public function getCmpReturn()
return $this->cmpReturn;
/**
* Set cmpReturnFrom
*
* @param string $cmpReturnFrom
* @return ComCompany
*/
public function setCmpReturnFrom($cmpReturnFrom)
$this->cmpReturnFrom = $cmpReturnFrom;
return $this;
/**
* Get cmpReturnFrom
*
* @return string
*/
public function getCmpReturnFrom()
return $this->cmpReturnFrom;
/**
* Set cmpReturnTill
*
* @param string $cmpReturnTill
* @return ComCompany
*/
public function setCmpReturnTill($cmpReturnTill)
$this->cmpReturnTill = $cmpReturnTill;
return $this;
/**
* Get cmpReturnTill
*
* @return string
*/
public function getCmpReturnTill()
return $this->cmpReturnTill;
/**
* Set cmpAddress
*
* @param string $cmpAddress
* @return ComCompany
*/
public function setCmpAddress($cmpAddress)
$this->cmpAddress = $cmpAddress;
return $this;
/**
* Get cmpAddress
*
* @return string
*/
public function getCmpAddress()
return $this->cmpAddress;
/**
* Set cmpPhone
*
* @param integer $cmpPhone
* @return ComCompany
*/
public function setCmpPhone($cmpPhone)
$this->cmpPhone = $cmpPhone;
return $this;
/**
* Get cmpPhone
*
* @return integer
*/
public function getCmpPhone()
return $this->cmpPhone;
/**
* Set cmpEmail
*
* @param string $cmpEmail
* @return ComCompany
*/
public function setCmpEmail($cmpEmail)
$this->cmpEmail = $cmpEmail;
return $this;
/**
* Get cmpEmail
*
* @return string
*/
public function getCmpEmail()
return $this->cmpEmail;
/**
* Set cmpSite
*
* @param string $cmpSite
* @return ComCompany
*/
public function setCmpSite($cmpSite)
$this->cmpSite = $cmpSite;
return $this;
/**
* Get cmpSite
*
* @return string
*/
public function getCmpSite()
return $this->cmpSite;
/**
* Set cmpCeo
*
* @param string $cmpCeo
* @return ComCompany
*/
public function setCmpCeo($cmpCeo)
$this->cmpCeo = $cmpCeo;
return $this;
/**
* Get cmpCeo
*
* @return string
*/
public function getCmpCeo()
return $this->cmpCeo;
/**
* Set cmpRegister
*
* @param string $cmpRegister
* @return ComCompany
*/
public function setCmpRegister($cmpRegister)
$this->cmpRegister = $cmpRegister;
return $this;
/**
* Get cmpRegister
*
* @return string
*/
public function getCmpRegister()
return $this->cmpRegister;
/**
* Set cmpUrl
*
* @param string $cmpUrl
* @return ComCompany
*/
public function setCmpUrl($cmpUrl)
$this->cmpUrl = $cmpUrl;
return $this;
/**
* Get cmpUrl
*
* @return string
*/
public function getCmpUrl()
return $this->cmpUrl;
/**
* Set cmpCategory
*
* @param \Catalog\WebBundle\Entity\ComCategory $cmpCategory
* @return ComCompany
*/
public function setCmpCategory(\Catalog\WebBundle\Entity\ComCategory $cmpCategory = null)
$this->cmpCategory = $cmpCategory;
return $this;
/**
* Get cmpCategory
*
* @return \Catalog\WebBundle\Entity\ComCategory
*/
public function getCmpCategory()
return $this->cmpCategory;
/**
* Set cmpCity
*
* @param \Catalog\WebBundle\Entity\ComCity $cmpCity
* @return ComCompany
*/
public function setCmpCity(\Catalog\WebBundle\Entity\ComCity $cmpCity = null)
$this->cmpCity = $cmpCity;
return $this;
/**
* Get cmpCity
*
* @return \Catalog\WebBundle\Entity\ComCity
*/
public function getCmpCity()
return $this->cmpCity;
【问题讨论】:
请发布您尝试访问城市的代码,以及您实体类的相关部分 你只有一个结果,所以它应该只是 $info->getCmpCity();对?或者如果您有多个结果,您是在问如何在一个数组中获取所有“cit”? @JasonRoman 我更新了帖子,现在你可以看到我的实体@Derick 我不是 hoave methos$info->getCmpCity()
请看我的代码。
【参考方案1】:
getResult()
将整个结果集作为可迭代数组集合返回,不是单个结果。因此,当您使用setMaxResults(1)
时,明智的做法是使用getSingleResult()
获取单个结果:
$info = $query->getSingleResult();
如果没有返回结果,$info
将是null
。否则,您将能够使用以下方法访问 cmpCity
属性:
$cmpCity = $info->getCmpCity();
同样,如果没有与 ComCompany
对象关联的 cmpCity
,$cmpCity
将是 null
。如果您想获取第一个 确实 具有关联 cmpCity
的 ComCompany
对象,请使用 innerJoin
而不是 leftJoin
:
$query = $em->createQueryBuilder()
->select('com', 'cit')
->from('CatalogWebBundle:ComCompany', 'com')
->innerJoin('com.cmpCity', 'cit')
->getQuery()
->setMaxResults(1);
$info = $query->getSingleResult();
$cmpCity = $info->getCmpCity();
【讨论】:
【参考方案2】:这可能与结合 leftJoin 和 setMaxResults 时的 issue 有关。
关于如何进行查询已经有了答案:https://***.com/a/14886847/3726645
【讨论】:
以上是关于Symfony 2 教义左连接的主要内容,如果未能解决你的问题,请参考以下文章