Symfony 2 Embedded forms using one to many db relationship

У меня проблема с встраиванием форм из разных сущностей в одну форму, моя форма отображается с firstname [input] lastname [input] address - но адрес не имеет ввода рядом с ним.

В основном я хочу создать форму, где пользователь может добавить имя, фамилию, адрес1, адрес2, город, страну и т.д. и отправить ее как одну, хотя это разные таблицы.

С основной формой проблем нет, единственная проблема у меня со второй встроенной формой. Любая помощь будет очень признательна.

Вот мой код:

Класс Member:

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;
}
}

Класс Address:

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;
    }
}

Форма 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';
    }


}

Форма Address:

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>

Давно пользуюсь этим сайтом, но это мой первый вопрос.

Спасибо

7
задан mizo 21 September 2011 в 21:13
поделиться