使用一对多数据库关系的Symfony 2 Embedded表单 [英] Symfony 2 Embedded forms using one to many db relationship

查看:86
本文介绍了使用一对多数据库关系的Symfony 2 Embedded表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将来自不同实体的表单嵌入到一个表单中时遇到问题,我的表单以名字[输入]姓[输入]地址显示-但该地址旁边没有输入.

I'm have a problem embedding forms from different entities in one form, my form is being displayed with firstname [input] lastname [input] address - but the address has no input next to it.

我基本上想创建一个表单,用户可以在其中添加名字,姓氏,地址1,地址2,城市,国家等,并将其作为一个表单提交,尽管它是不同的表.

Basically I want to create a form where the user can add first name, last name, address1, address2, city, country ect and submit it it as one, although it's different tables.

主要形式没有问题,我唯一遇到的问题是第二种嵌入式形式.任何帮助将不胜感激.

The main form is no problem the only issue I'm having is with the second embedded form. Any help would be greatly appreciated.

这是我的代码:

会员等级:

namespace Pomc\MembersBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Pomc\MembersBundle\Entity\Member
*/
class Member
{
/**
 * @var integer $id
 */
private $id;

/**
 * @var string $firstName
 */
private $firstName;

/**
 * @var string $lastName
 */
private $lastName;

/**
 * @var Pomc\MembersBundle\Entity\Address
 */
private $address;

/**
 * @var Pomc\MembersBundle\Entity\Telephone
 */
private $telephone;

public function __construct()
{
    $this->address = new \Doctrine\Common\Collections\ArrayCollection();
    $this->telephone = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set firstName
 *
 * @param string $firstName
 */
public function setFirstName($firstName)
{
    $this->firstName = $firstName;
}

/**
 * Get firstName
 *
 * @return string 
 */
public function getFirstName()
{
    return $this->firstName;
}

/**
 * Set lastName
 *
 * @param string $lastName
 */
public function setLastName($lastName)
{
    $this->lastName = $lastName;
}

/**
 * Get lastName
 *
 * @return string 
 */
public function getLastName()
{
    return $this->lastName;
}

/**
 * Add address
 *
 * @param Pomc\MembersBundle\Entity\Address $address
 */
public function addAddress(\Pomc\MembersBundle\Entity\Address $address)
{
    $this->address[] = $address;
}

/**
 * Get address
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getAddress()
{
    return $this->address;
}

/**
 * Add telephone
 *
 * @param Pomc\MembersBundle\Entity\Telephone $telephone
 */
public function addTelephone(\Pomc\MembersBundle\Entity\Telephone $telephone)
{
    $this->telephone[] = $telephone;
}

/**
 * Get telephone
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getTelephone()
{
    return $this->telephone;
}
}

这是地址类别:

namespace Pomc\MembersBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Pomc\MembersBundle\Entity\Address
 */
class Address
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $addressType
     */
    private $addressType;

    /**
     * @var string $firstLine
     */
    private $firstLine;

    /**
     * @var string $secondLine
     */
    private $secondLine;

    /**
     * @var string $city
     */
    private $city;

    /**
     * @var string $postCode
     */
    private $postCode;

    /**
     * @var string $country
     */
    private $country;

    /**
     * @var Pomc\MembersBundle\Entity\Member
     */
    private $member;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set addressType
     *
     * @param string $addressType
     */
    public function setAddressType($addressType)
    {
        $this->addressType = $addressType;
    }

    /**
     * Get addressType
     *
     * @return string 
     */
    public function getAddressType()
    {
        return $this->addressType;
    }

    /**
     * Set firstLine
     *
     * @param string $firstLine
     */
    public function setFirstLine($firstLine)
    {
        $this->firstLine = $firstLine;
    }

    /**
     * Get firstLine
     *
     * @return string 
     */
    public function getFirstLine()
    {
        return $this->firstLine;
    }

    /**
     * Set secondLine
     *
     * @param string $secondLine
     */
    public function setSecondLine($secondLine)
    {
        $this->secondLine = $secondLine;
    }

    /**
     * Get secondLine
     *
     * @return string 
     */
    public function getSecondLine()
    {
        return $this->secondLine;
    }

    /**
     * Set city
     *
     * @param string $city
     */
    public function setCity($city)
    {
        $this->city = $city;
    }

    /**
     * Get city
     *
     * @return string 
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set postCode
     *
     * @param string $postCode
     */
    public function setPostCode($postCode)
    {
        $this->postCode = $postCode;
    }

    /**
     * Get postCode
     *
     * @return string 
     */
    public function getPostCode()
    {
        return $this->postCode;
    }

    /**
     * Set country
     *
     * @param string $country
     */
    public function setCountry($country)
    {
        $this->country = $country;
    }

    /**
     * Get country
     *
     * @return string 
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set member
     *
     * @param Pomc\MembersBundle\Entity\Member $member
     */
    public function setMember(\Pomc\MembersBundle\Entity\Member $member)
    {
        $this->member = $member;
    }

    /**
     * Get member
     *
     * @return Pomc\MembersBundle\Entity\Member 
     */
    public function getMember()
    {
        return $this->member;
    }
}

这是成员表单:

namespace Pomc\MembersBundle\Form\Type;

use \Symfony\Component\Form\AbstractType;
use \Symfony\Component\Form\FormBuilder;

class MemberType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('firstName');
        $builder->add('lastName');
        $builder->add('address','collection', array( 'type' =>  new AddressType(),
                                              'allow_add' => true,
                                              'prototype' => true,
                                              'by_reference' => false,
                                              ));
    }

    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Pomc\MembersBundle\Entity\Member');
    }
    /**
     * Returns the name of this type.
     *
     * @return string The name of this type
     */
    function getName()
    {
        return 'member';
    }


}

这是地址表格:

namespace Pomc\MembersBundle\Form\Type;

use \Symfony\Component\Form\AbstractType;
use \Symfony\Component\Form\FormBuilder;


class AddressType extends AbstractType
{
    public function buildForm(Formbuilder $builder, array $options)
    {
        $builder->add('firstLine');

    }

    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Pomc\MembersBundle\Entity\Address');
    }
    /**
     * Returns the name of this type.
     *
     * @return string The name of this type
     */
    function getName()
    {
        return 'address';
    }

    function getIdentifier()
    {
        return 'address';
    }
}

这是控制器:

namespace Pomc\MembersBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use \Pomc\MembersBundle\Entity\Member;
use \Symfony\Component\HttpFoundation\Request;
use \Pomc\MembersBundle\Form\Type\MemberType;

class DefaultController extends Controller
{

    public function indexAction($name)
    {
        return $this->render('PomcMembersBundle:Default:index.html.twig', array('name' => $name));
    }

    public function newAction(Request $request)
    {

        $member = new Member();

        $form = $this->get('form.factory')->create(new MemberType());

        if($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);

            if($form->isValid())
            {
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($member);
                $em->flush();

            }
        }
        return $this->render('PomcMembersBundle:Default:new.html.twig', array( 'form'=> $form->createView(),));

    }
}

这是模板:

<form action="{{ path('member_new') }}" method="post" {{ form_enctype(form)}}>
    {{ form_widget(form) }}
    <div>
    {{ form_row(form.address)}}
    </div>
    <input type="submit" />
</form>

这个网站的长期用户,但这是我的第一个问题.

Been a long time user of this site, but this is my first question.

谢谢

推荐答案

哦,我遇到了同样的问题,但是我找到了解决方案,希望对您有所帮助:-)

Oh I faced the same problem, but I found the solution, hope this will help you :-)

您忘记了将Address对象添加到成员实体.

You're forgetting to add an Address object to the member entity.

在执行操作时,您需要执行以下操作:

In your action you'll need to do the following:

$member = new Member();
$member->addAddress(new Address());

$form = $this->createForm(new MemberType(), $member);

然后在您的模板中:

 {% for address in form.address %}
  {{ form_widget(address.firstLine) }}
 {% endfor %}

您的第一线"小部件与实体属性无关.

Btw your 'firstline' widget doesn't relate to an entity property.

顺便说一句,如果您两次调用addAddress,您当然会在表单中得到两个"firstline"小部件.

Btw if you called addAddress two times, you would of course get two 'firstline' widgets in your form.

希望这行得通.祝你好运.

Hope this works. best of luck.

这篇关于使用一对多数据库关系的Symfony 2 Embedded表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆