1

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

Molecular Man
  • 22,277
  • 3
  • 72
  • 89
Mark Cibor
  • 2,737
  • 4
  • 21
  • 23

1 Answers1

3

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

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

Nanocom
  • 3,696
  • 4
  • 31
  • 46