脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Doctrine QueryBuilder选择所有列?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的控制器中有这个查询
$query = $this->getDoctrine()->getReposITory('WebBundle:Store') ->createQueryBuilder('s') ->select('t.name,COUNT(s) as counter') ->groupBy('s.town') ->leftJoin('s.owner','o') ->leftJoin('s.town','t') ->where('s.owner = :id') ->orderBy('t.name','ASC') ->setParameter('id',$id) ->getQuery(); $list = $query->getResult();
有没有办法从Town选择所有列而不是声明每一列?类似于 – >选择(‘t.*,COUNT(s)作为计数器’).我可以选择我现在需要的,但对于较大的表我将需要其他方法.
我试过 – >选择(‘t,COUNT(s)作为计数器’)但我收到了异常错误.
{% for town in list %} <span>{{ town.name }}</b> [{{ town.counter }}]</span> {% enDFor %}
感谢大家的建议!
所有者必须与商店有1-n的关系.
因此,您的所有者实体将如下所示:
class Owner { PRotected $stores; // ... public function __construct() { $this->stores = new ArrayCollection(); } public function getStores() { return $this->stores; } public function setStores($stores) { $this->stores = new ArrayCollection(); foreach ($stores as $store) { $this->stores->add($store); } return $this; } public function addStore(Store $store) // ... can use $this->store->add() public function removeStore(Store $store) // ... can use $this->store->removeElement() // etc ... }
所以现在,您可以使用Collection :: count()Doctrine方法!
$storesCnt = $user->getStores()->count();
您想获得用户和城镇的所有商店吗?
没问题 ! Collection :: filter()是你的朋友!
$storesForAUserAndAGivenTown = $user->getStores()->filter(function (Store $store) use ($town) { return ($store->getTown() === $town); });
而已.
考虑Doctrine的第一个规则是忘记数据库!所以只使用DQL或QueryBuilder,并且只在必要时使用.
希望它会对你有所帮助.
以上是脚本宝典为你收集整理的php – 如何使用Doctrine QueryBuilder选择所有列?全部内容,希望文章能够帮你解决php – 如何使用Doctrine QueryBuilder选择所有列?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。