如何在表单集合symfony2上的OneToMany(用户)内的OneToMany(UserAddress)内解决ManyToOne(AddressType)

时间:2017-05-19 19:43:52

标签: php symfony doctrine-orm formcollection

我正在使用symfony2表单集合,在应用程序的任何位置我都有此错误:

cannot convert AddressType to string

我的用户实体

/**
 * @var $addresses
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserAddress", 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 \AppBundle\Entity\UserAddress $address
 *
 * @return User
 */
public function addAddress(\AppBundle\Entity\UserAddress $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 \AppBundle\Entity\UserAddress $address
 */
public function removeAddress(\AppBundle\Entity\UserAddress $address)
{
    if(! $address instanceof UserAddress) {
        throw new InvalidArgumentException("$address must be an instance of UserAddress");
    }

    $this->addresses->removeElement($address);
}

/**
 * Get addresses
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getAddresses()
{
    return $this->addresses;
}

我的用户地址实体

/**
 * @var
 * Many addresses have one address type.
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\AddressType", inversedBy="userAddressType")
 * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
 */
private $addressType;

(...)

/**
 * Add addressType
 *
 * @param \AppBundle\Entity\AddressType $addressType
 *
 * @return UserAddress
 */
public function addAddressType(\AppBundle\Entity\AddressType $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 \AppBundle\Entity\AddressType $addressType
 */
public function removeAddressType(\AppBundle\Entity\AddressType $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 \AppBundle\Entity\AddressType $addressType
 *
 * @return UserAddress
 */
public function setAddressType(\AppBundle\Entity\AddressType $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 \AppBundle\Entity\AddressType
 */
public function getAddressType()
{
    return $this->addressType;
}

我的地址类型实体

/**
 * @var
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserAddress", mappedBy="addressType")
 */
private $userAddressType;

(...)

/**
 * Add userAddressType
 *
 * @param \AppBundle\Entity\UserAddress $userAddressType
 *
 * @return AddressType
 */
public function addUserAddressType(\AppBundle\Entity\UserAddress $userAddressType)
{
    $this->userAddressType[] = $userAddressType;

    return $this;
}

/**
 * Remove userAddressType
 *
 * @param \AppBundle\Entity\UserAddress $userAddressType
 */
public function removeUserAddressType(\AppBundle\Entity\UserAddress $userAddressType)
{
    $this->userAddressType->removeElement($userAddressType);
}

/**
 * Get userAddressType
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUserAddressType()
{
    return $this->userAddressType;
}

这是Form UserType:

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

1 个答案:

答案 0 :(得分:0)

我看到这是因为我的Entity类没有正确的armeabi-v7a方法,所以/libs/armeabi-v7a无法呈现正确的“值”和吐出全部的异常代替代表性字符串的对象。