Php 如何迭代symfony 3.findby()结果集

Php 如何迭代symfony 3.findby()结果集,php,symfony,doctrine-orm,twig,Php,Symfony,Doctrine Orm,Twig,我不熟悉symfony/doctor,在设置第一个表和查询时,我正在努力寻找在Twig中输出结果的最佳方法 因此,我在\AppBundle\Controller\BrandsController上有这个方法 public function showAction($brand) { $product = $this->getDoctrine() ->getRepository('AppBundle:Brands') ->findOneB

我不熟悉symfony/doctor,在设置第一个表和查询时,我正在努力寻找在Twig中输出结果的最佳方法

因此,我在\AppBundle\Controller\BrandsController上有这个方法

    public function showAction($brand)
{

   $product = $this->getDoctrine()
       ->getRepository('AppBundle:Brands')
       ->findOneByBrand($brand);

   if (!$product) {
       throw $this->createNotFoundException(
           'No product found for id '.$brand
       );
   }
    return $this->render('brands/brands.html.twig', [
        'product' => $product
    ]);
}
这将生成一个如下所示的对象,我无法对其进行迭代

Brands {#459 ▼
-brand_id: 24
-brand: "Ford"
-active: "Y"
-img_logo: "/img/brand/ford.png"
-img_logo_small: "/img/brand/ford_20.png"
-img_logo_big: "/img/brand/ford-big.png"
}
当然,我可以创建如下查询,但这否定了findBy()方法的好处:

我发现了类似的问题,但他们给出了如下数组,从而搞乱了数组键:

array:6 [▼
"\x00AppBundle\Entity\Brands\x00brand_id" => 24
"\x00AppBundle\Entity\Brands\x00brand" => "Ford"
"\x00AppBundle\Entity\Brands\x00active" => "Y"
"\x00AppBundle\Entity\Brands\x00img_logo" => "/img/brand/ford.png"
"\x00AppBundle\Entity\Brands\x00img_logo_small" => "/img/brand/ford_20.png"
"\x00AppBundle\Entity\Brands\x00img_logo_big" => "/img/brand/ford-big.png"
]
顺便说一下,这是brands/brands.html.twig上代码的简单版本:

 {% for item in product %}
      <p> This is my   {{ item }}</p>
 {% endfor %}
{%用于产品%中的项目]
这是我的{{item}

{%endfor%}
有没有干净的方法


谢谢你,将军…

像数组一样在细枝中迭代您的
产品
,这是一个坏主意。只是因为如果产品处于活动状态或未处于活动状态,您将需要更多的控制,如显示/执行此操作,这只是时间问题。。。等等所以很快你就得做这样的事情(只是一个可能的用例的野生例子…)

在你的回购课上

public function findOneForViewByBrand( $brand )
{

  // you ca make use of PARTIAL SELECTS
  // NOTE: if you use PARTIAL selects you have to select at least id, like PARTIAL p.{brand_id}

  $query = $this->createQueryBuilder('p')
    ->select('PARTIAL p.{brand_id, brand, active, img_logo, img_logo_small, img_logo_big}')
    ->where('p.brand = :brand')
    ->setParameter('brand', $brand)
    ->getQuery();


  // If found you'll get a Brand BUT ONLY field in curly brackets will be populated with data, others will be just NULL
  return $query->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); 


}
然后在你的小树枝上

{% for fieldName, fieldValue in product %}
      {# should skip null values #}
      {% if fieldValue is defined and fieldValue is not empty % }
      <p> This is my   {{ fieldValue }}</p>
     {% endif %}
 {% endfor %}
{%fieldName,fieldValue在product%}
{#应跳过空值#}
{%如果定义了fieldValue且fieldValue不为空%}
这是我的{{fieldValue}

{%endif%} {%endfor%}
关于


请注意,代码未经测试,它就在这里,因此您可以获得一个想法;)我不知道您使用的symfony版本和安装的条令版本,因此,如果上述方法之一不存在,请不要生气:)

常规… 像数组一样在细枝中迭代您的
产品
,这是一个坏主意。只是因为如果产品处于活动状态或未处于活动状态,您将需要更多的控制,如显示/执行此操作,这只是时间问题。。。等等所以很快你就得做这样的事情(只是一个可能的用例的野生例子…)

在你的回购课上

public function findOneForViewByBrand( $brand )
{

  // you ca make use of PARTIAL SELECTS
  // NOTE: if you use PARTIAL selects you have to select at least id, like PARTIAL p.{brand_id}

  $query = $this->createQueryBuilder('p')
    ->select('PARTIAL p.{brand_id, brand, active, img_logo, img_logo_small, img_logo_big}')
    ->where('p.brand = :brand')
    ->setParameter('brand', $brand)
    ->getQuery();


  // If found you'll get a Brand BUT ONLY field in curly brackets will be populated with data, others will be just NULL
  return $query->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); 


}
然后在你的小树枝上

{% for fieldName, fieldValue in product %}
      {# should skip null values #}
      {% if fieldValue is defined and fieldValue is not empty % }
      <p> This is my   {{ fieldValue }}</p>
     {% endif %}
 {% endfor %}
{%fieldName,fieldValue在product%}
{#应跳过空值#}
{%如果定义了fieldValue且fieldValue不为空%}
这是我的{{fieldValue}

{%endif%} {%endfor%}
关于


请注意,代码未经测试,它就在这里,因此您可以获得一个想法;)我不知道您使用的symfony版本和安装的条令版本,因此,如果上述方法之一不存在,请不要生气:)

要使用实体序列化程序条令脚本,请查看以下链接:
只需将脚本复制到一个新类中:
EntitySerializer.php
,在
AppBundle\Controller
下,然后在BrandsController类中:

$em = $this->getDoctrine()->getManager(); 
$product = $em->getRepository('AppBundle:Brands') ->findOneByBrand($brand); 
$serializer = new EntitySerializer($em);
$product = $serializer->toArray($product);
最后,您可以轻松地迭代数组:

foreach ($array as $key => $value) {
    echo 'the key is '.$key.'<br>';
    echo 'the value is '.$value;
}
foreach($key=>$value的数组){
回显“键是“.$key”。
”; 回显“值为”。$value; }
要使用实体序列化程序原则脚本,请查看以下链接:
只需将脚本复制到一个新类中:
EntitySerializer.php
,在
AppBundle\Controller
下,然后在BrandsController类中:

$em = $this->getDoctrine()->getManager(); 
$product = $em->getRepository('AppBundle:Brands') ->findOneByBrand($brand); 
$serializer = new EntitySerializer($em);
$product = $serializer->toArray($product);
最后,您可以轻松地迭代数组:

foreach ($array as $key => $value) {
    echo 'the key is '.$key.'<br>';
    echo 'the value is '.$value;
}
foreach($key=>$value的数组){
回显“键是“.$key”。
”; 回显“值为”。$value; }
您不需要将其转换为数组,只需{{product.id}{{{product.brand}}等即可输出您的小树枝文件中的对象。@Bhs。那会很尴尬,必须提到每一把钥匙。这应该使用for循环并在对象/数组上迭代,如:{%for product%}这是我的{{item}

{%endfor%}您需要根据对象构造数组以进行迭代,看看github上的serlize脚本:@Bhs,序列化程序脚本对我有用。把这个作为一个正确的答案,我会接受它,谢谢。你不需要把它转换成数组,在你的树枝文件中输出对象,只要{{{product.id}{{{product.brand}}等等。@Bhs。那会很尴尬,必须提到每一把钥匙。这应该使用for循环并在对象/数组上迭代,如:{%for product%}这是我的{{item}

{%endfor%}您需要根据对象构造数组以进行迭代,看看github上的serlize脚本:@Bhs,序列化程序脚本对我有用。把这个作为一个正确的答案,我会接受它,谢谢。谢谢@V-Light。我很欣赏你的全面见解,但有时你只是想让这件简单的事情起作用。如果有一个像findby()这样的工具,人们应该能够在不过度复杂的情况下使用它。虽然不是最优的,但是Bhs提到的序列化程序脚本完成了这项工作,它将在我的应用程序和其他地方继续可用,只要需要这个简单的操作。谢谢@V-Light。我很欣赏你的全面见解,但有时你只是想让这件简单的事情起作用。如果有一个像findby()这样的工具,人们应该能够在不过度复杂的情况下使用它。虽然不是最优的,但是Bhs提到的序列化程序脚本完成了这项工作,它将在我的应用程序和其他地方,只要需要这个简单的操作,都可以使用。其想法是使用findby()而不是查询生成器。因此,我将跳过您的介绍,只添加一些代码,如$em=$this->getDoctrine()->getManager()$product=$em->getRepository('AppBundle:Brands')->findOneByBrand($brand)$serializer=新的EntitySerializer($em)$product=$serializer->toArray($product);修正了findoneBy或findByBrand,最后我们需要返回一个对象将其转换为数组