Doctrine - How to extract results and their relationships as an array

I have an object, name it Stones and Stones has ManyToMany relationship with attributes.

So, I query the object to get the Stones, and then I moisten it to convert it to an array.

    $result =  $this->stoneRepository->find($stone_id);

    if ( ! $result )
    {
        return false;
    }

    $resultArray =  $this->doctrineHydrator->extract($result);

This works fine for the Stone object, but I noticed that the connection (attributes) remains as objects.

array (size=12)
  'id' => int 1
  'name' => string 'Agate' (length=5)
  'title' => string 'Title' (length=5)
'attribute' => 
    array (size=5)
      0 => 
        object(Stone\Entity\StAttribute)[1935]
          private 'id' => int 2
          private 'name' => string 'Hay fevor' (length=9)
          private 'state' => boolean true
          private 'created' => null
          private 'modified' => null
      1 => 
        object(Stone\Entity\StAttribute)[1936]
          private 'id' => int 15
          private 'name' => string 'Libra' (length=5)
          private 'state' => boolean true
          private 'created' => null
          private 'modified' => null
      2 => 

and etc.

What is the process of hydrating Attribute objects?

+4
source share
2 answers

Hydration fills an object (object) using an array that is the opposite of extraction.

, , ORM .

Query Builder Api find() . , , :

$qb = $this->stoneRepository->createQueryBuilder('S');
$query = $qb->addSelect('A')
            ->leftJoin('S.attribute', 'A')
            ->where('S.id = :sid')
            ->setParameter('sid', (int) $stone_id)
            ->getQuery();

$resultArray = $query->getOneOrNullResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

, SQL- . (StAttribute )

+4

, , , CustomStrategy.

foozy. , , ApiGility, , , .

, , :

<?php
namespace Api\V1\Rest\Stone;

use DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;

class CustomStrategy extends AbstractCollectionStrategy
{

    public function __construct($hydrator)
    {
        $this->hydrator = $hydrator;
    }

    /**
     * @param mixed $values
     * @return array|mixed
     */
    public function extract($values)
    {
        $returnArray = [];

        foreach ($values AS $value)
        {

            $returnArray[] =  $this->hydrator->extract($value);
        }

        return $returnArray;

    }

    /**
     * @param mixed $values
     * @return mixed
     */
    public function hydrate($values)
    {
        $returnArray = [];

        foreach ($values AS $value )
        {
            $returnArray[] = $this->hydrator->hydrate($value);
        }

        return $returnArray;
    }
}

, :

$result =  $this->stoneRepository->find($stone_id);

$this->doctrineHydrator->addStrategy("product", new CustomStrategy( $this->doctrineHydrator ) );
$this->doctrineHydrator->addStrategy("attribute", new CustomStrategy( $this->doctrineHydrator ) );
$this->doctrineHydrator->addStrategy("image", new CustomStrategy( $this->doctrineHydrator ) );
$this->doctrineHydrator->addStrategy("related", new CustomStrategy( $this->doctrineHydrator ) );

$resultArray =  $this->doctrineHydrator->extract($result);

:

<?php
namespace Api\V1\Rest\Stone;

class StoneEntity
{
    public $id;
    public $name;
    public $description;
    public $code;
    public $attribute;
    public $product;
    public $image;

    public function getArrayCopy()
    {
        return array(
            'id'          => $this->id,
            'name'        => $this->name,
            'description' => $this->description,
            'code'        => $this->code,
            'attribute'   => $this->attribute,
            'product'     => $this->product,
            'image'       => $this->image
        );
    }

    public function exchangeArray(array $array)
    {
        $this->id           = $array['id'];
        $this->name         = $array['name'];
        $this->description  = $array['description'];
        $this->code         = $array['code'];
        $this->attribute    = $array['attribute'];
        $this->product      = $array['product'];
        $this->image        = $array['image'];
    }
}

- :

    $entity = new StoneEntity();
    $entity->exchangeArray($resultArray);

, , :

    return $entity;

, , foozy :

public function fetchOne($stone_id)
    {
        $qb = $this->stoneRepository->createQueryBuilder('S');
        $query = $qb->addSelect('A','P','I','C')
            ->leftJoin('S.attribute', 'A')
            ->innerJoin('A.category', 'C')
            ->innerJoin('S.product' , 'P')
            ->innerJoin('S.image' , 'I')
            ->where('S.id = :sid')
            ->setParameter('sid', (int) $stone_id)
            ->getQuery();

        $resultArray = $query->getOneOrNullResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

        if ( ! $resultArray )
        {
            return false;
        }

        return $resultArray;
    }
0

Source: https://habr.com/ru/post/1568376/


All Articles