gpt4 book ai didi

php - Symfony2 Doctrine 在查询中预加载

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:25 28 4
gpt4 key购买 nike

我使用 Symfony 2.4.2 我有两个 Doctrine 实体。实体名称:城市和城镇。

关系结构:

城市 -> 城镇 = OneToMany城镇 -> 城市 = 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;

Where 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=>City Query(Not Working) 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 查询(不工作)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);

最佳答案

如果您想在存储库中执行此操作,您可以执行 JOIN 并将资源添加到所选对象。

这是 QueryBuilder 版本:

$qb = $this->createQueryBuilder('city')
->addSelect('town')
->innerJoin('town.city', 'city');
$cities = $qb->getQuery()->getResult();

关于php - Symfony2 Doctrine 在查询中预加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694729/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com