我使用Symfony 2.4.2我有两个Doctrine实体。名称:城市和城镇。
关系结构:
城市 - > Town = OneToMany 镇 - > City = ManyToOne
实体/ City.php
/**
* @ORM\OneToMany(targetEntity="Town", mappedBy="city")
*/
private $towns;
实体/ Town.php
/**
* @ORM\ManyToOne(targetEntity="City", inversedBy="towns")
* @ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
其中In Query未在第二和第三项中运行。
1-)Town => City Query(Working)Profiler SS:http://prntscr.com/34lx9q
$query = $this->_entityManager->createQuery('SELECT town FROM PersonalSiteBundle:Town town');
$query->setFetchMode(
'Personal\SiteBundle\Entity\Town',
"city",
\Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER
);
$query->execute();
2-)Town =>城市查询(不工作)Profiler SS:http://prntscr.com/34ly4h
$query = $this->_entityManager->createQuery('SELECT town FROM PersonalSiteBundle:Town town');
$query->setFetchMode(
'Personal\SiteBundle\Entity\Town',
"city",
\Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER
);
$query->execute(array(),Query::HYDRATE_ARRAY);
3-)City => Town Query(Not Working)Profiler SS:http://prntscr.com/34ly4h
$query = $this->_entityManager->createQuery('SELECT city FROM PersonalSiteBundle:City city');
$query->setFetchMode(
'Personal\SiteBundle\Entity\City',
"towns",
\Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER
);
$query->execute(array(),Query::HYDRATE_ARRAY);
答案 0 :(得分:3)
如果要在存储库中执行此操作,可以执行JOIN并将资源添加到所选对象。
这是QueryBuilder版本:
ALTER TABLE car_table MODIFY price decimal(10,3) AFTER color;