Doctrine 2: Hydrate key-value array

doctrine indexby
doctrine getscalarresult
doctrine hydration
doctrine join
doctrine hydrate array
doctrine hydrate entity from array
doctrine orm
doctrine getoneornullresult

Why this query doesn't work?

    $query = $this->_em->createQuery('
        SELECT 
            COUNT(p) AS products_count
        FROM 
            Application\Entity\Category c
            INDEX BY c.id
            LEFT JOIN c.products p
        GROUP BY
            c.id
    ');

    $rows = $query->getSingleScalarResult();

I expected to get the result as: "category_id => products_count" array, but it throws an Doctrine\ORM\NonUniqueResultException without any message.

If you COUNT the rows in a query that is GROUPED, it will return multiple results with the count for each group. In your case, you will get the counts for each Category.

If you try to getSingleScalarResult() on a result which has more than one entries, it will produce a Doctrine\ORM\NonUniqueResultException. Instead, you should use getScalarResult() and iterate through the entries.

Edit:

To transform the results to the form array("category_id" => "products_count_value"), you can use the following code:

$output = array();
foreach ($rows as $category, $values) {
    $output[$category] = $values['products_count'];
}

$output should now contain the values the way you want them.

Defining a column to be the key of the result hydrated as array in, array � doctrine2. It's really simple, just use "INDEX BY" and specify the column in your query: $em = $this->getEntityManager(); $query� Doctrine Array to Entity Hydrator Introduction. A hydrator for doctrine 2 that converts an array to the entity of your choice. Installing via Composer. The recommended way to install is through Composer. composer require pmill/doctrine-array-hydrator Example. Given this doctrine entity:

This trait will give you the result in the expected format for simple entity fields. After useing in your repository, you can use it like:

$em->getRepository('AppBundle:Branch')->getIndexedList('id', 'name');

But, for your case, as it has some aggregation result, you may write a similar function in your repository.

(Though it's a very old question, posting it here so that it may give a hint to someone who will be searching for a similar idea.)

Doctrine Query Language, In the example above, each element of the result array would be an array of the scalar When Doctrine hydrates a query with fetch-join it returns the class in the � Doctrine offers a few different ways to execute the queries and a few different ways to hydrate the data. It can hydrate the data as objects, php arrays which is much much faster than using objects or it can simply skip the hydration process all together.

As others have already said this is not supported by Doctrine by default. You could use PHP's array_column function for this:

$query = $this->_em->createQuery('
    SELECT 
        COUNT(p) AS products_count, c.id AS category_id
    FROM 
        Application\Entity\Category c
        LEFT JOIN c.products p
    GROUP BY
        c.id
');

$pairs = array_column($query->getArrayResult(), 'products_count', 'category_id');

Data Hydrators, Sometimes though you want to hydrate the data to an array, use no hydration or return a single scalar value. This chapter aims to document and demonstrate the � Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Use a column value of a Doctrine Entity as key of an array result , <?php use PHPSnippets\Db\Entity\Test; // Index by uses a given value as result array key // Be sure to use only primary or unique keys // #### QueryBuilder ### � When Doctrine hydrates a query with fetch-join it returns the class in the FROM clause on the root level of the result array. In the previous example an array of User instances is returned and the address of each user is fetched and hydrated into the User#address variable. If you access the address Doctrine does not need to lazy load the

How to get a Doctrine2 result object as an associative array?, $accounts = $qb->getQuery()->getResult( Doctrine\ORM\Query:: HYDRATE_ARRAY );. If you are using findOneBy() then it will always return an entity. Due to the� Since each element of a collection based on IDictionary<TKey,TValue> is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is KeyValuePair<TKey,TValue> .

andreia/awesome-doctrine: A collection of useful Doctrine , Defining a Column to be the Key of the Result Hydrated as Array c INDEX BY c. name'); $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); $this-> conn->executeUpdate('INSERT INTO table2 (field1, field2) VALUES(?, ?)' ZF2 + Doctrine: Read data from a database via TableGateway and hydrate Doctrine entity including its related objects with it 7 Doctrine - Hydrate collection in Entity class

Comments
  • So, I used wrong method for fetching data. If I use getScalarResult, the result looks like: "category_id" => array("products_count" => "products_count_value"), but I need to fetch data in the following format: "category_id" => "products_count_value", without extra iteration. Is it possible?
  • Well, it depends on what exactly you are trying to achieve. Right now you get the number of products for each category. Do you want the overall number of products? Or the number of products for a specific category?
  • I need to get the number of products for each category in simple format: category_id => products_count.
  • I don't think that's possible out of the box. However, you can transform the result to fit to your specifications. Please look at my edit.
  • Thanks, I use array_map for that. I just wanted to get confirmation that there's no out of the box solution.