将一个表单嵌入到另一个symfony2中 [英] embed one form into another symfony2

查看:189
本文介绍了将一个表单嵌入到另一个symfony2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体形式叫做订单和地址。我想将地址表单嵌入到订单中。两个实体都与用户列有关系。



地址实体

  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;

订单实体

 类订单
{
/ **
* @ ORM\Id
* @ ORM\Column(type =整数)
* @ 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;

订单表单

 命名空间Root\ContestBundle\Form\Front; 

使用Symfony\Component\Form\AbstractType;
使用Symfony\Component\Form\FormBuilderInterface;
使用Symfony\Component\OptionsResolver\OptionsResolverInterface;
使用Root\ContestBundle\Entity\Address;
使用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');

}

但是我收到以下错误

 在渲染模板期间抛出异常(属性地址或方法getAddress()也不是方法isAddress() 存在于类Root\ContestBundle\Entity\Orders)

那么什么错误我已经在我的代码中帮助我出去

解决方案

也许太晚了,但这里是我的答案。
我几天前发现symfony,所以我不是专家。
有几件事情似乎让我很开心。



在地址实体,我想你应该这样做:

  / ** @ 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;
}

在订单实体上,我认为你有这样的想法:

  / ** 
* @ 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->地址;
}

最后在你的OrderType,我想你应该有:

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

希望能帮助你。


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

解决方案

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.

这篇关于将一个表单嵌入到另一个symfony2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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