embed one form into another symfony2

时光总嘲笑我的痴心妄想 提交于 2019-12-23 05:23:58

问题


I have two entity forms called 'Orders' and 'Address'. I want to embed Address form into orders form. Both entities are having relation by user column.

Address Entity

class Address
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=128)
     */
    protected $type;    

    /**
     * @ORM\ManyToOne(targetEntity="Root\UserBundle\Entity\User", inversedBy="address")
     * @ORM\JoinColumn(name="user", referencedColumnName="id")
     * @ORM\ManyToOne(targetEntity="Orders", inversedBy="address")
     * @ORM\JoinColumn(name="user", referencedColumnName="user")
     */     
    protected $user;       

Orders Entity

class Orders
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=128)
     */
    protected $status;    

    /**
     * @ORM\ManyToOne(targetEntity="Root\UserBundle\Entity\User", inversedBy="orders")
     * @ORM\JoinColumn(name="user", referencedColumnName="id")
     * @ORM\OneToMany(targetEntity="Address", mappedBy="orders")
     * @ORM\JoinColumn(name="user", referencedColumnName="user")
     */     
    protected $user;     

Orders Form

namespace Root\ContestBundle\Form\Front;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Root\ContestBundle\Entity\Address;
use Root\ContestBundle\Form\Front\AddressType;
class OrdersType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('address', 'collection', array('type' => new AddressType()));
        $builder
        ->add('termsAccepted');

    }

But I am getting error like below

An exception has been thrown during the rendering of a template ("Neither property "address" nor method "getAddress()" nor method "isAddress()" exists in class "Root\ContestBundle\Entity\Orders"") 

So what mistake I have made in my code. Help me out


回答1:


Maybe it's too late but here is my answer. I discovered symfony few days ago so I am no expert. There are few things that seems akward to me.

On Adress Entity, i think you should do that :

/** @ORM\OneToMany(targetEntity="Order", mappedBy="adress") */
protected $orders; 

public function addOrder(Order $order){
    $this->orders[] = $order;
}

public function removeOrder(Order $order){
    $this->orders->removeElement($order);
}

public function getOrders(){
    return $this->orders;
}

On Order Entity, I think you sould have that :

/**
 * @ORM\ManyToOne(targetEntity="Address", inversedBy="orders")
 * @ORM\JoinColumn(name="idAdress", referencedColumnName="id")
 */     
protected $adress;

public function setAdress($adress){
    $this->adress = $adress;
}

public function getAdress(){
    return $this->adress;
}

And last on you OrderType, I think you should have that :

public function buildForm(FormBuilderInterface $builder, array $options){
    $builder->add('adress',new AdressType());
}

Hope that will help you.



来源:https://stackoverflow.com/questions/14853709/embed-one-form-into-another-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!