# Annotation composed objected field is not inserted in database via Doctrine

**URL:** <https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820>\
**Category:** Components & MVC\
**Tags:** laminas-form, laminas-mvc, doctrine\
**Created:** [March 18, 2022, 7:07am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820 "2022-03-18T07:07:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [March 18, 2022, 7:07am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/1 "2022-03-18T07:07:26Z")

</div>

Hi, I’ve got a form with annotations. The form fields are showing but somehow the data is not inserted in the database. There is no error when submitting the form either for the composed object. Only the data for the non-composed object/parent is inserted correctly.  
The annotation form looks like below:

```auto
/**
 * @Annotation\Name("Post")
 * @Annotation\Hydrator("Laminas\Hydrator\ObjectPropertyHydrator")
 */

class Post
{
    /**
     * @Annotation\Exclude()
     */
    public $id;
    
    /**
     * @Annotation\Filter({"name":"StringTrim"})
     * @Annotation\Validator({"name":"StringLength", "options":{"min":5, "max":255}})
     * @Annotation\Attributes({"type":"text"})
     * @Annotation\Options({"label":"Title:"})
     */
    public $title;
    
    /**
     * @Annotation\Type("Laminas\Form\Element\Textarea")
     * @Annotation\Options({"label":"Description:"})
     */
    public $body;
    
    /**
     * @Annotation\Name("posts")
     * @Annotation\ComposedObject( {"target_object":"Post\Form\Annotation\Category", "is_collection":"true"} )
     */
    public $categories;

}

```

The post entity code looks like below:

```auto
/**
 * Post
 * 
 * @Gedmo\Loggable
 * @ORM\Table(
 * name = "posts",
 * indexes = {
 * @ORM\Index(name="title_idx", columns={"title"})
 * }
 * )
 * @ORM\Entity(repositoryClass="Articles\Repository\PostRepository")
 */
class Post
{
    
    /**
     * @var integer
     *
     * @ORM\Column(type="integer", options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @Gedmo\Versioned
     * @ORM\Column(type="string", length=255)
     */
    protected $title;

    /**
     * @var string
     *
     * @Gedmo\Versioned
     * @ORM\Column(type="text")
     */
    protected $body;

    /**
     * @var string
     *
     * @Gedmo\Versioned
     * @Gedmo\Slug(fields={"title"}, updatable=false)
     * @ORM\Column(type="string", length=255)
     */
    protected $slug;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="posts")
     * @ORM\JoinTable(
     * name="post_category",
     * joinColumns={
     * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
     * },
     * inverseJoinColumns={
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     * }
     * )
     */
    protected $categories;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->categories = new ArrayCollection();
    }
    //...//
}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 18, 2022, 7:18am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/2 "2022-03-18T07:18:28Z")

</div>

> [@ALTAMASH80](#):
>
> Only the data for the non-composed object/parent is inserted correctly.

Is the correct data present in the entities before they are inserted?

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [March 18, 2022, 7:45am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/3 "2022-03-18T07:45:03Z")

</div>

Yes, I can add/edit Post entity data but the composed object(Category) data is totally ignored. I can add/edit Category data via a single category form. But not having it as a composed object. Thanks!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 18, 2022, 8:22am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/4 "2022-03-18T08:22:41Z")

</div>

> [@ALTAMASH80](#):
>
> …but the composed object(Category) data is totally ignored.

Ignored by what? The form?

```php
$form = (new Laminas\Form\Annotation\AnnotationBuilder)->createForm(Post::class);

$object = new Post();
$form->bind($object);

$form->setData(['…']);

if ($form->isValid()) {
    var_dump($object); // Is the correct data present here?
}
```

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [March 18, 2022, 10:29am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/5 "2022-03-18T10:29:13Z")

</div>

Hi @froschdesign,

I’ve created the form exactly as you’ve created it. The output for the Post Entity Object is like below.

```auto
object(Posts\Entity\Post)#575 (10) { ["id":protected]=> NULL ["title":protected]=> string(35) "Laminas Project community" ["body":protected]=> string(35) "Laminas Project community" ["slug":protected]=> NULL ["created_at":"Posts\Entity\Post":private]=> NULL ["updated_at":"Posts\Entity\Post":private]=> NULL ["categories":protected]=> object(Doctrine\Common\Collections\ArrayCollection)#569 (1) { ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(0) { } } ["created_by":protected]=> NULL ["modified_by":protected]=> NULL }

```

Thanks!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 18, 2022, 10:57am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/6 "2022-03-18T10:57:52Z")

</div>

> [@ALTAMASH80](#):
>
> I’ve created the form exactly as you’ve created it.

Unfortunately, there is a misunderstanding here, because you should not rebuild the part from me, but you can say if your object is correctly filled with data at the point after validation.

I do not know your objects and have not looked at the annotation completely. But you will be able to tell by yourself if your object is correctly filled with the correct data at this point before it is processed further.

This allows us to further narrow down the circle of possible sources of error.

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [March 18, 2022, 11:07am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/7 "2022-03-18T11:07:41Z")

</div>

Thanks for the reply @froschdesign. The category object within Post Entity is empty. So, I’ve some mistake a mistake in the annotation? If that is the case I’ll dig it further. Thanks!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 18, 2022, 11:29am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/8 "2022-03-18T11:29:01Z")

</div>

> [@ALTAMASH80](#):
>
> The category object within Post Entity is empty.

Is the title and the body set on your `Post` entity?

* * *

You define your form via annotations in a separate class but at the end the entity class must be filled with data.  
Maybe I’m wrong here but how should the `ObjectPropertyHydrator` work for your `Post` entity here? See the documentation:

> The ObjectPropertyHydrator hydrates objects and extracts data using publicly accessible properties which match a key in the data set.

> **[Quick Start - laminas-hydrator - Laminas Docs](https://docs.laminas.dev/laminas-hydrator/v4/quick-start/#objectpropertyhydrator)**
>
> Serialize objects to arrays, and vice versa

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [March 21, 2022, 7:49am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/9 "2022-03-21T07:49:56Z")

</div>

Hi @froschdesign,  
I’m using DoctrineORM, doctrine provides a doctrine hydrator which I used like below:

```auto
//Post:class is Post Annotation Form Class not Post Entity Class.
$form = (new Laminas\Form\Annotation\AnnotationBuilder)->createForm(Post::class);
$form->setHydrator(new \Doctrine\Laminas\Hydrator\DoctrineObject);

```

The form data is represented like below:

 ![Screenshot from 2022-03-21 12-44-34](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/e/ef502141e81370fbd7d5e67da71061032c877ace.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 21, 2022, 12:38pm UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/10 "2022-03-21T12:38:36Z")

</div>

Your first post says something different, that’s why I asked.

> [@ALTAMASH80](#):
>
> ` * @Annotation\Hydrator("Laminas\Hydrator\ObjectPropertyHydrator")`

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 25, 2022, 8:11am UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/11 "2022-03-25T08:11:58Z")

</div>

> [@ALTAMASH80](#):
>
> The form data is represented like below:
> 
> ![Screenshot from 2022-03-21 12-44-34](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/e/ef502141e81370fbd7d5e67da71061032c877ace.png)

In your form data you have `Category` but the related form element has the name `posts`?!

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [March 29, 2022, 12:40pm UTC](https://discourse.laminas.dev/t/annotation-composed-objected-field-is-not-inserted-in-database-via-doctrine/2820/12 "2022-03-29T12:40:00Z")

</div>

Hi @froschdesign, thanks for the reply. I think I’ve got a problem with my code. So, I’ll share it with you and let me know if I’m right or wrong.

1. I’m creating my form from **“Laminas\Form\Annotation\AnnotationBuilder”**. Is that a correct thing to do when using doctrine? Because doctrine suggest using **"\DoctrineORMModule\Form\Annotation\AnnotationBuilder($entityManager);"**
2. The second problem is that I’m trying to create an embedded form altogether rather than a select box for a category. Is that the reason the category field is getting neglected entirely?

The reason is when I created a form with doctrine, which came up with other issues and I couldn’t solve all of them. But, I was able to create a form. The form that doctrine annotation created was a form with category as a select box and it named the field categories as it is in post entity. But, my issue from start is to insert post data with a new category name. That means I’ve to create multiple category input boxes.

Thanks!
