我有两个名为'Orders'和'Address'的实体形式。我想将地址表格嵌入订单表格中。两个实体都具有用户列的关系。
地址实体
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"")
所以我在代码中犯了什么错误。帮帮我
答案 0 :(得分: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());
}
希望能帮助你。