0

我有两个实体表格,分别称为“订单”和“地址”。我想将地址表格嵌入到订单表格中。两个实体都与用户列有关系。

地址实体

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;       

订单实体

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;     

订单表格

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');

    }

但我收到如下错误

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"") 

那么我在代码中犯了什么错误。帮帮我

4

1 回答 1

3

也许为时已晚,但这是我的答案。几天前我发现了 symfony,所以我不是专家。对我来说似乎很尴尬的事情很少。

在 Adress Entity 上,我认为你应该这样做:

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

最后是您的 OrderType,我认为您应该拥有:

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

希望对您有所帮助。

于 2013-03-03T12:07:48.757 回答