尝试设置两个简单对象Purchase
和Address
。
一次购买可以有多个地址(购买地址,发票地址)
来自地址实体的简化ORM如下所示:
X\Entity\Address:
repositoryClass: X\Repository\AddressRepository
type: entity
manyToOne:
purchase:
targetEntity: Purchase
inversedBy: addresses
joinColumn:
name: purchase_id
referencedColumnName: id
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
firstname:
type: string
length: 255
email:
type: string
length: 255
lifecycleCallbacks: { }
购买实体的简化ORM如下:
X\Entity\Purchase:
repositoryClass: X\Repository\PurchaseRepository
type: entity
oneToMany:
addresses:
targetEntity: Address
mappedBy: purchase
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
quantity:
type: integer
orderedAt:
type: datetime
toBeDeliveredAt:
type: datetime
lifecycleCallbacks: { }
现在,购买类中的这些行给了我错误
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getAddresses()
{
return $this->addresses;
}
话说:
属性“地址”和方法“getAddress()”之一, “address()”,“isAddress()”,“hasAddress()”,“__ get()”存在并具有 “X \ Entity \ Purchase”类中的公共访问。
这是绝对正确的,没有属性address
,并且没有任何方法可以存在该非现有属性。
问题是属性名称是addresses
,并且有一个方法getAddresses
但是出了点问题。
现在问题是为什么“系统”(symfony?)搜索属性名称address
?当然,我该如何解决这个问题?
更新
表单提交时出错。控制器如下所示:
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$purchase = new Purchase();
$form = $this->createFormBuilder($purchase)
->add('address', AddressType::class)
->add('quantity', IntegerType::class)
->add('toBeDeliveredAt', DateType::class)
->add('comment', TextareaType::class)
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($purchase);
$em->flush();
}
return $this->render('X:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
}
AddressType如下所示:
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class)
->add('email', EmailType::class);
}
}
感谢您的帮助!
如果需要,整个购买类看起来像这样:
<?php
namespace X\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Purchase
*/
class Purchase
{
/**
* @var int
*/
private $id;
/**
* @var int
*/
private $quantity;
/**
* @var \DateTime
*/
private $orderedAt;
/**
* @var \DateTime
*/
private $toBeDeliveredAt;
/**
* @var bool
*/
private $delivered;
/**
* @var \DateTime
*/
private $deliveredAt;
/**
* @var bool
*/
private $payd;
/**
* @var \DateTime
*/
private $paydAt;
/**
* @var string
*/
private $comment;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $addresses;
/**
*
*/
public function __construct()
{
$this->addresses = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set quantity
*
* @param integer $quantity
* @return Purchase
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set orderedAt
*
* @param \DateTime $orderedAt
* @return Purchase
*/
public function setOrderedAt($orderedAt)
{
$this->orderedAt = $orderedAt;
return $this;
}
/**
* Get orderedAt
*
* @return \DateTime
*/
public function getOrderedAt()
{
return $this->orderedAt;
}
/**
* @param \DateTime $toBeDeliveredAt
*/
public function setToBeDeliveredAt($toBeDeliveredAt)
{
$this->toBeDeliveredAt = $toBeDeliveredAt;
}
/**
* @return \DateTime
*/
public function getToBeDeliveredAt()
{
return $this->toBeDeliveredAt;
}
/**
* @param Address $address
* @return Address
*/
public function addAddress(Address $address)
{
$this->addresses[] = $address;
if($address->getPurchase() !== $this) {
$address->setPurchase($this);
}
return $this;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getAddresses()
{
return $this->addresses;
}
}
答案 0 :(得分:1)
错误来自您的PurchaseType中的->add('address', AddressType::class)
。
试试这个:
->add('addresses', AddressType::class)
答案 1 :(得分:0)
我也面临同样的问题。但我能够解决这个问题。
只需检查您的entity.orm.yml文件,然后从您要创建关系的form
字段中删除该字段。
$builder->add('question')->add('demographicYesNo')->add('questionSetId')->add('questionOptionTypeId')->add('questionSliderRange');
所以,我已经删除了
加( 'questionSetId') - &gt;添加( 'questionOptionTypeId')
因为我在这个字段上创建了关系,默认情况下symfony在Form中添加它。
谢谢,
Anirudh Sood。