Symfony2许多实体的一种形式多对一关系

时间:2016-09-07 13:30:39

标签: php forms symfony controller entity

我真的需要表单问题的帮助。 我有主要实体 - 用户:

class User {
private $id;
private $name;
private $surname;
private $description;
private $photo;

/**
 * @ORM\OneToMany(targetEntity="Adress", mappedBy="user", cascade={"persist", "remove"})
 */
private $adresses;

/**
 * @ORM\ManyToMany(targetEntity="Groups", inversedBy="users")
 * @ORM\JoinTable(name="users_groups")
 */
private $groups;

我得到,设置,__构建等...

然后地址实体:

class Adress {
private $id;
private $city;
private $street;
private $number;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="adresses", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

Adress的形式:

class AdressType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('city')
            ->add('street')
            ->add('number')
            ->add('user')
    ;
}

等..(由CRUD生成)

用户表格:

    public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('name')
            ->add('surname')
            ->add('description')
            ->add('photo', 'file', array('label' => 'Photo (files: .jpg, .png)', 'data_class' => null))
            ->add('groups')
            ->add('adresses', 'collection', array(
                'type' => new AdressType(),
                'allow_add' => true,
                'allow_delete' => true,
                'cascade_validation' => true,
                'by_reference' => false,
            ))

新用户的控制器:

    private function createCreateForm(User $entity) {

    $adress = new Adress();
    $entity->addAdress($adress);

    $form = $this->createForm(new UserType(), $entity, array(
        'action' => $this->generateUrl('user_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}
   public function createAction(Request $request) {
    $entity = new User();

    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        //add Photo
        /**
         * @var Symfony\Component\HttpFoundation\File\UploadedFile $file 
         */
        $photo = $entity->getPhoto();

        //generate name for photo
        $photoName = md5(uniqid()) . '.' . $photo->guessExtension();

        $photo->move($this->getParameter('photo_directory'), $photoName);

        $entity->setPhoto($photoName);

        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );
}

结果如下: Click Here 我不知道如何解决这个问题我想在一个表单中添加两个实体(User,Adress)的信息。但我不知道我应该如何处理AdressType中的'用户' - 因为它显示为选择字段,但我想在此地址和此用户之间添加新关系。

任何人都可以帮助我 - 我花了几个小时而且无法弄明白。 :)谢谢!

0 个答案:

没有答案