1

I'm integrating Doctrine2 into CodeIgniter.

My Entity class News.php

<?php

namespace Models\Entities;

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

And my model class News_model.php

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

class News_model extends CI_Model {
    //Model code here
}

When I use $news = $this->em->getRepository('Entities:News')->findAll() in News_model class and printed, var_dump($news), I get an array of object(Models\Entities\News), like follow:

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
)

But I expected an associative array, like follow:

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)
 )

How can I convert the Doctrine Entity object (first showed array) result to a PHP associative array (second showed array)?

3 Answers 3

1

You are working with Doctrine ORM. ORM means Object Relational Mapper. You use ORM because you want to get results as objects. Otherwise you better start to read about Doctrine DBAL. Then this line:

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

If you use findAll() then you expect an Collection of objects. In Doctrine we talk about collections instead of array's.

Those collections you can simple walk through with a foreach just like a normal array. Then you can use each object inside the collection which has some benefits: especial directly call some custom methods

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

foreach($newsitems as $newsitem)
{
    echo '<h3>' . $newsitem->getTitle() . '</h3>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

I agree with @Frank B, the reason you use Doctrine is that you get to work with objects instead of a magic array.

However, if you are set on having an array, you can use the Symfony Serializer to convert any object to an array.

Just add some annotations to your entity:

use Symfony\Component\Serializer\Annotation\Groups;

class News {
    /**
     * @Groups({"group1"})
     */
    protected $id;

    /**
     * @Groups({"group1"})
     */
    protected $title;

    /**
     * @Groups({"group1"})
     */
    protected $slug;
}

Then you can convert your array collection like this:

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

Comments

1

why don't you use native doctrine method getArrayResult in your class repository?

In your controller :

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

In your class Repository :

class NewsRepository extends \Doctrine\ORM\EntityRepository
{
    public function yourMethodName()
    {
        $query = $this->createQueryBuilder('n');

        /***/

        return $query->getQuery()->getArrayResult();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.