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 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭