我怎样才能在表单集合symfony2上解决OneToOne(UserAddress)中的OneToOne(UserAdd



我正在使用symfony2表单集合,并且在应用程序的任何点上我都遇到此错误:

cannot convert AddressType to string

我的用户实体

/**
* @var $addresses
* @ORMOneToMany(targetEntity="AppBundleEntityUserAddress", mappedBy="user", cascade={"persist"})
*/
private $addresses;

(...

/**
* Set addresses
*
* @param Collection $addresses
* @return User
*/
public function setAddresses(Collection $addresses) : User
{
if(! $addresses instanceof Collection){
throw new InvalidArgumentException("$addresses must be an instance of Collection");
}
$this->addresses = $addresses;
return $this;
}
/**
* Add address
*
* @param AppBundleEntityUserAddress $address
*
* @return User
*/
public function addAddress(AppBundleEntityUserAddress $address)
{
if(! $address instanceof UserAddress) {
throw new InvalidArgumentException("$address must be an instance of UserAddress");
}
if(!$this->addresses->contains($address)) {
$address->setUser($this);
$this->emails->add($address);
}
$this->addresses[] = $address;
return $this;
}
/**
* Remove address
*
* @param AppBundleEntityUserAddress $address
*/
public function removeAddress(AppBundleEntityUserAddress $address)
{
if(! $address instanceof UserAddress) {
throw new InvalidArgumentException("$address must be an instance of UserAddress");
}
$this->addresses->removeElement($address);
}
/**
* Get addresses
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAddresses()
{
return $this->addresses;
}

我的用户地址实体

/**
* @var
* Many addresses have one address type.
* @ORMManyToOne(targetEntity="AppBundleEntityAddressType", inversedBy="userAddressType")
* @ORMJoinColumn(name="type_id", referencedColumnName="id")
*/
private $addressType;

(...

/**
* Add addressType
*
* @param AppBundleEntityAddressType $addressType
*
* @return UserAddress
*/
public function addAddressType(AppBundleEntityAddressType $addressType)
{
if(! $addressType instanceof UserType && $addressType !== null){
throw new InvalidArgumentException("$addressType must be an instance of AddressType, or null");
}
$this->addressType[] = $addressType;
return $this;
}
/**
* Remove addressType
*
* @param AppBundleEntityAddressType $addressType
*/
public function removeAddressType(AppBundleEntityAddressType $addressType)
{
if(! $addressType instanceof UserType && $addressType !== null){
throw new InvalidArgumentException("$addressType must be an instance of AddressType, or null");
}
$this->addressType->removeElement($addressType);
}
/**
* Set addressType
*
* @param AppBundleEntityAddressType $addressType
*
* @return UserAddress
*/
public function setAddressType(AppBundleEntityAddressType $addressType = null)
{
if(! $addressType instanceof User && $addressType !== null) {
throw new InvalidArgumentException("$addressType must be an instance of AddressType, or null");
}
$this->addressType = $addressType;
return $this;
}
/**
* Get addressType
*
* @return AppBundleEntityAddressType
*/
public function getAddressType()
{
return $this->addressType;
}

我的地址类型实体

/**
* @var
*
* @ORMOneToMany(targetEntity="AppBundleEntityUserAddress", mappedBy="addressType")
*/
private $userAddressType;

(...

/**
* Add userAddressType
*
* @param AppBundleEntityUserAddress $userAddressType
*
* @return AddressType
*/
public function addUserAddressType(AppBundleEntityUserAddress $userAddressType)
{
$this->userAddressType[] = $userAddressType;
return $this;
}
/**
* Remove userAddressType
*
* @param AppBundleEntityUserAddress $userAddressType
*/
public function removeUserAddressType(AppBundleEntityUserAddress $userAddressType)
{
$this->userAddressType->removeElement($userAddressType);
}
/**
* Get userAddressType
*
* @return DoctrineCommonCollectionsCollection
*/
public function getUserAddressType()
{
return $this->userAddressType;
}

这是在表单用户类型中:

->add('addresses', CollectionType::class,
[
'entry_type' => UserAddressType::class,
'by_reference' => false,
'allow_add' => true,
'allow_delete' => true,
]
);

问题是,例如,当我在editAction中运行时,会发生此错误

Cannot convert AddressType to string

感谢您的帮助!


编辑:

我找到了答案阅读

http://symfony.com/doc/current/form/events.html#pre-populating-the-form-formevents-pre-set-data-and-formevents-post-set-data

我认为这是由于我的实体类没有正确的__toString()方法造成的,因此select无法呈现正确的"值"和吐出完整对象代替代表性字符串的异常。

相关内容

  • 没有找到相关文章

最新更新