Mark Cibor
Mark Cibor

Reputation: 2807

Symfony2 Relation two entity, arraycollection

I have created 2 entities
User and Photo

Now I want to create one-to-many relation.
Suppose I have this code in the User entity class:

// User.php
     /**
     * @ORM\OneToMany(targetEntity="Photo", mappedBy="user")
     */
    protected $photos;

    public function __construct()
    {
        $this->photos = new ArrayCollection();
    }

when i add the photo's form into user's form, similarly to how it's done in this code

// UserType.php
    public function buildForm(FormBuilder $builder, array $options)
    {
        // ...
        $builder->add('photos', new PhotoType());
    }

it throws:

Expected argument of type "Acme\UserBundle\Entity\Photo", "Doctrine
\Common\Collections\ArrayCollection" given

so how can I add photo's form into user's form?

ps sorry for my english

Upvotes: 1

Views: 2924

Answers (1)

Nanocom
Nanocom

Reputation: 3726

You are mistaken in your form builder : you need a collection of PhotoType:

$builder->add('photos', 'collection', array('type' => new PhotoType()));

Upvotes: 3

Related Questions