天天看点

symfony中如何把实体对象数据转换为数组数据?

解决办法: 使用getArrayResult 转换为数组

存储类 代码

<?php

// src/AppBundle/Repository/ProductRepository.php
namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * 商品的自定义存储类
*/
class ProductRepository extends EntityRepository
{
    public function findAllOrderedByName()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT p FROM AppBundle:Product p ORDER BY p.name ASC'
            )->getArrayResult();
    }
}
           

控制器代码:

public function listAction(){
        $products = $this->getDoctrine()
                ->getRepository(Product::class)
                ->findAllOrderedByName();

        dump($products);
        
    }
           

处理结果:

symfony中如何把实体对象数据转换为数组数据?