【问题标题】:How to convert Doctrine array to PHP associative array如何将 Doctrine 数组转换为 PHP 关联数组
【发布时间】:2017-09-02 02:05:07
【问题描述】:

我正在将 Doctrine2 集成到 CodeIgniter 中。

我的实体类 News.php

<?php

namespace Models\Entities;

/**
 * News
 *
 * @Table(name="news", indexes={@Index(name="slug", columns={"slug"})})
 * @Entity
 */
class News {
     //HERE: properties, getter, setter, etc.
}

还有我的模型类 News_model.php

<?php
require_once(APPPATH."models/entities/News.php");
use Models\Entities\News;

class News_model extends CI_Model {
    //Model code here
}

当我在 News_model 类中使用 $news = $this->em->getRepository('Entities:News')->findAll() 并打印 var_dump($news) 时,我得到一个对象数组(Models\ Entities\News),如下所示:

array (size=6)
  0 => 
    object(Models\Entities\News)[87]
      private 'id' => int 1
      private 'title' => string 'text here' (length=9)
      private 'slug' => string '' (length=0)
      private 'text' => string 'text here' (length=9)
      private 'news' => null
)

但我希望有一个关联数组,如下所示:

array (size=6)
  0 => 
    array (size=4)
      'id' => string '1' (length=1)
      'title' => string 'text here' (length=9)
      'slug' => string '' (length=0)
      'text' => string 'text here' (length=9)
 )

如何将 Doctrine Entity 对象(第一个显示的数组)结果转换为 PHP 关联数组(第二个显示的数组)?

【问题讨论】:

    标签: php arrays codeigniter symfony doctrine-orm


    【解决方案1】:

    你为什么不在你的类库中使用原生学说方法getArrayResult

    在您的控制器中:

    /***/
    $news = $this->em->getRepository('Entities:News')->yourMethodName();
    /***/
    

    在你的类存储库中:

    class NewsRepository extends \Doctrine\ORM\EntityRepository
    {
        public function yourMethodName()
        {
            $query = $this->createQueryBuilder('n');
    
            /***/
    
            return $query->getQuery()->getArrayResult();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我同意@Frank B,您使用 Doctrine 的原因是您可以使用对象而不是魔术数组。

      但是,如果您打算使用数组,则可以使用Symfony Serializer 将任何对象转换为数组。

      只需为您的实体添加一些注释:

      use Symfony\Component\Serializer\Annotation\Groups;
      
      class News {
          /**
           * @Groups({"group1"})
           */
          protected $id;
      
          /**
           * @Groups({"group1"})
           */
          protected $title;
      
          /**
           * @Groups({"group1"})
           */
          protected $slug;
      }
      

      然后你可以像这样转换你的数组集合:

      $news = $this->em->getRepository('Entities:News')->findAll();
      $serializer = $this->getContainer()->get('serializer');
      $newsArray = $serializer->normalize($news, 'json', ['groups' => ['group1']]);
      

      【讨论】:

        【解决方案3】:

        您正在使用 Doctrine ORM。 ORM 表示对象关系映射器。您使用 ORM 是因为您希望将结果作为对象获取。否则你最好开始阅读 Doctrine DBAL。 然后这一行:

         $news = $this->em->getRepository('Entities:News')->findAll();
        

        如果你使用 findAll() 那么你期望一个对象的集合。在 Doctrine 中,我们谈论的是 collections 而不是数组。

        您可以像普通数组一样使用 foreach 轻松遍历这些集合。然后你可以使用集合中的每个对象,这有一些好处:特别是直接调用一些自定义方法

        $newitems = $this->em->getRepository('Entities:News')->findAll();
        
        foreach($newsitems as $newsitem)
        {
            echo '<h3>' . $newsitem->getTitle() . '</h3>';
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-08
          • 2018-04-22
          • 1970-01-01
          • 2017-07-27
          • 1970-01-01
          • 1970-01-01
          • 2012-05-16
          • 1970-01-01
          相关资源
          最近更新 更多