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条回答 默认 最新

相关推荐 更多相似问题