embed one form into another symfony2

2019-06-10 06:18发布

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条回答
做个烂人
2楼-- · 2019-06-10 06:49

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.

查看更多
登录 后发表回答