# API - Embed object in collection? - mezzio-hal

**URL:** <https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879>\
**Category:** Mezzio\
**Tags:** mezzio, mezzio-hal\
**Created:** [October 9, 2020, 1:09pm UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879 "2020-10-09T13:09:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [October 9, 2020, 1:09pm UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879/1 "2020-10-09T13:09:29Z")

</div>

Hi,

I try to develop a API Rest service with mezzio and have a problem.  
I used [https://github.com/ezimuel/zend-expressive-api](https://github.com/ezimuel/zend-expressive-api) from @enrico that i migrated to Mezzio for my base of structure and testing.

I have a Product model and a Stock model.

When i’m requesting for **one product** i would like a Stock model embedded. It works!  
I got a product with a embed Stock object inside in my json. I proceeded like that:

```
   // Product model query
    $product = $this->productModel->getProduct($id);

    // Create product HAL from object
    $productHal = $this->resourceGenerator->fromObject($product, $request);

    // Stock model query
    $stock = $this->stockModel->getWithProductId($id);

    // Create stock HAL from object
    $stockHal = $this->resourceGenerator->fromObject($stock, $request);

    // Embed stock to product
    $productHal = $productHal->embed('stocks', [$stockHal]);

    return $this->responseFactory->createResponse($request, $productHal);

```

**But how to do that in a collection?**

I have something like that in my Product model but don’t kwow how to add embed Stock in a collection of Product, i know the process have to be done somewhere else…

```
public function getAll(): Paginator
{
    $select = new Select($this->table->getTable());

    $select->columns($this->fields)
            ->where->equalTo('isActive', 1); 

    $paginatorAdapter = new DbSelect(
        $select,
        $this->table->getAdapter(),
        $this->table->getResultSetPrototype()
    );

    $paginator = new ProductCollection($paginatorAdapter);
    return $paginator;
}

```

I read somewhere that i have to create a special hydrator for it… Is it right?

```
  class ProductHydrator extends ObjectPropertyHydrator
   {
    private $stockModel;

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

    public function hydrate(array $data, object $object)
    {
        $product = parent::hydrate($data, $object);

        if (null !== $product->id) {
            $stock = $this->stockModel->getWithProductId($product->id);
            $product->stocks = $stock;
        }

        return $product;
    }
 }

```

**It works but i would like to know if it is the right way? Or it exist another way to manage that?**

Thanks!

---

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [October 19, 2020, 12:20pm UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879/2 "2020-10-19T12:20:29Z")

</div>

I did not get answer… someone can tell me please if i’m on the wight way with my problem (inject data in model from hydrator)? Thanks!

---

<div class="post-metadata">

**Author:** ![guidofaecke](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/guidofaecke/32/427_2.png) [@guidofaecke](https://discourse.laminas.dev/u/guidofaecke)\
**Post date:** [October 22, 2020, 6:06pm UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879/3 "2020-10-22T18:06:20Z")

</div>

I might don’t have the answer for you, but I ran into a similar problem with Doctrine. The answer to that was a SQL JOIN.

---

<div class="post-metadata">

**Author:** ![Serge\_Nguimjeu](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/serge_nguimjeu/32/562_2.png) [@Serge\_Nguimjeu](https://discourse.laminas.dev/u/Serge_Nguimjeu)\
**Post date:** [November 27, 2025, 8:09pm UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879/4 "2025-11-27T20:09:20Z")

</div>

I’m having the same issue. Can you guys please share your solution?

---

<div class="post-metadata">

**Author:** ![Tyrsson](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tyrsson/32/1664_2.png) [@Tyrsson](https://discourse.laminas.dev/u/Tyrsson)\
**Post date:** [November 30, 2025, 4:12am UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879/5 "2025-11-30T04:12:34Z")

</div>

Just make sure I understand your question, you are using Mezzio, Laminas Paginator and Laminas Db correct?

---

<div class="post-metadata">

**Author:** ![Serge\_Nguimjeu](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/serge_nguimjeu/32/562_2.png) [@Serge\_Nguimjeu](https://discourse.laminas.dev/u/Serge_Nguimjeu)\
**Post date:** [November 30, 2025, 12:23pm UTC](https://discourse.laminas.dev/t/api-embed-object-in-collection-mezzio-hal/1879/6 "2025-11-30T12:23:32Z")

</div>

Hi @Tyrsson ,

I’m using the following:

- mezzio
- laminas paginator
- doctrine
- laminas hydrator

For single entities, I use the resource generator and the response factory like the OP in order to have HAL embedded entities, but for collections I haven#t found a way to also have HAL embedded entities.
