dongzhuxun5136 2016-07-27 09:31
浏览 71
已采纳

一对多关键 - 未捕获的PHP异常Symfony \ Component \ PropertyAccess \ Exception \ NoSuchPropertyException

Trying to set up two simple objects Purchase and Address.

One Purchase can have multiple Addresses (purchase address, invoice address)

Simplified ORM from Address Entity looks like:

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: {  }

Simplified ORM from Purchase Entity looks like:

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: {  }

Now these lines from the Purchase Class give me error

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

saying:

Neither the property "address" nor one of the methods "getAddress()", "address()", "isAddress()", "hasAddress()", "__get()" exist and have public access in class "X\Entity\Purchase".

Which is absolutely correct, there is no Property address and there aren't any methods for that non existing property.

Problem is kind of the Property name is addresses and there is a method getAddresses but something goes wrong with that.

Now the question is why is "the system" (symfony?) searching for a property name address? And of course how can I solve this?

Update

The error occurs on form submission. The Controller looks like this:

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(),
        ));
    }
}

And the AddressType looks like this:

class AddressType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', TextType::class)
            ->add('email', EmailType::class);
    }
}

Thanks for helping!

If needed the entire Purchase Class looks like this:

<?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;
    }
}
  • 写回答

2条回答 默认 最新

  • dqotv26286 2016-07-27 10:01
    关注

    The error comes from ->add('address', AddressType::class) in your PurchaseType.

    Try this:

    ->add('addresses', AddressType::class)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 SQLServer怎么录入下标
  • ¥100 无网格伽辽金方法研究裂纹扩展的程序
  • ¥15 错误于library(org.Hs.eg.db): 不存在叫‘org.Hs.eg.db’这个名称的程序包,如何解决?
  • ¥60 求一个图片处理程序,要求将图像大小跟现实生活中的大小按比例联系起来的
  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活