简体   繁体   English

使用EntityType表单构建器时,Symfony无法确定访问类型

[英]Symfony Could not determine access type when using EntityType form builder

I have 2 entities Cars and Parts and I want to be able to create new Car with multiple parts. 我有2个实体的CarsParts ,我希望能够创建具有多个零件的新汽车。 For this reason I made this form in CarsType 因此,我在CarsTypeCarsType了此表单

$builder->
        add('make')->
        add('model')->
        add('travelledDistance')->
        add('parts',EntityType::class,array(
            'class' => Parts::class,
            'choice_label'=>"name",
            'query_builder' => function(PartsRepository $partsRepository){
                return $partsRepository->getAllPartsForCarsForm();
            },
            'multiple' => true
        ));

It gives me this error 它给我这个错误

Could not determine access type for property "parts" in class "AppBundle\\Entity\\Cars". 无法确定类“ AppBundle \\ Entity \\ Cars”中属性“ parts”的访问类型。

Here is my entity Cars 这是我的实体Cars

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Cars
 *
 * @ORM\Table(name="cars")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CarsRepository")
 */
class Cars
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Make", type="string", length=255)
     */
    private $make;

    /**
     * @var string
     *
     * @ORM\Column(name="Model", type="string", length=255)
     */
    private $model;

    /**
     * @var int
     *
     * @ORM\Column(name="TravelledDistance", type="bigint")
     */
    private $travelledDistance;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
     * @ORM\JoinTable(
     *  name="Parts_Cars",
     *  joinColumns={
     *      @ORM\JoinColumn(name="Part_Id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="Car_Id", referencedColumnName="id")
     *  })
     */
    private $parts;
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set make
     *
     * @param string $make
     *
     * @return Cars
     */
    public function setMake($make)
    {
        $this->make = $make;

        return $this;
    }

    /**
     * Get make
     *
     * @return string
     */
    public function getMake()
    {
        return $this->make;
    }

    /**
     * Set model
     *
     * @param string $model
     *
     * @return Cars
     */
    public function setModel($model)
    {
        $this->model = $model;

        return $this;
    }

    /**
     * Get model
     *
     * @return string
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * Set travelledDistance
     *
     * @param integer $travelledDistance
     *
     * @return Cars
     */
    public function setTravelledDistance($travelledDistance)
    {
        $this->travelledDistance = $travelledDistance;

        return $this;
    }

    /**
     * Get travelledDistance
     *
     * @return int
     */
    public function getTravelledDistance()
    {
        return $this->travelledDistance;
    }

    /**
     * @return mixed
     */
    public function getParts()
    {
        return $this->parts;
    }

}

And in case it matters here is my Parts entity 如果这很重要,那是我的Parts实体

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Parts
 *
 * @ORM\Table(name="parts")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PartsRepository")
 */
class Parts
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="Price", type="decimal", precision=10, scale=2)
     */
    private $price;

    /**
     * @var int
     *
     * @ORM\Column(name="Quantity", type="integer")
     */
    private $quantity;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Suppliers", inversedBy="parts")
     * @ORM\JoinColumn(name="Supplier_Id", referencedColumnName="id")
     */
    private $supplier;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Cars", mappedBy="parts")
     */
    private $cars;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Parts
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set price
     *
     * @param string $price
     *
     * @return Parts
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return string
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set quantity
     *
     * @param integer $quantity
     *
     * @return Parts
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;

        return $this;
    }

    /**
     * Get quantity
     *
     * @return int
     */
    public function getQuantity()
    {
        return $this->quantity;
    }

    /**
     * @return mixed
     */
    public function getSupplier()
    {
        return $this->supplier;
    }

    /**
     * @return mixed
     */
    public function getCars()
    {
        return $this->cars;
    }
    public function __toString()
    {
       return $this->getName();
    }

}

To save time here is the mapping between the 2 entities as well as the property parts in Cars 为了节省时间,这里是两个实体之间的映射以及Cars的属性parts

/**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
     * @ORM\JoinTable(
     *  name="Parts_Cars",
     *  joinColumns={
     *      @ORM\JoinColumn(name="Part_Id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="Car_Id", referencedColumnName="id")
     *  })
     */
    private $parts;

and In Parts Parts

 /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Cars", mappedBy="parts")
     */
    private $cars;

Here is the newAction in CarsController 这是newAction中的CarsController

/**
     * Creates a new car entity.
     *
     * @Route("/new", name="cars_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $carsRepository = $this->getDoctrine()->getManager()->getRepository('AppBundle:Cars');
        $car = new Cars();
        $form = $this->createForm('AppBundle\Form\CarsType', $car);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($car);
            $em->flush();

            return $this->redirectToRoute('cars_show', array('id' => $car->getId()));
        }

        return $this->render('cars/new.html.twig', array(
            'car' => $car,
            'form' => $form->createView(),
        ));
    }

My question is - How can I make it so that I can create new Car with several Parts and to save the relationship in the database so that when I retrieve the Car I can get the parts as well? 我的问题是-如何做到这一点,以便可以创建具有多个零件的新Car并将关系保存到数据库中,以便在检索Car时也可以获取零件?

Basically how to make it so when I create a new car the relationship is saved in the parts_cars table which holds the id's? 基本上,如何做到这一点,以便在我创建新车时将关系保存在保存ID的parts_cars表中?

Let Doctrine do the JOIN work 让Doctrine做JOIN工作

Since you're doing a ManyToMany (EntityToEntity), the @JoinColumn directives are not needed. 由于您正在执行ManyToMany(EntityToEntity),因此@JoinColumn指令。

Try removing it from the Cars entity. 尝试将其从Cars实体中删除。

Cars 汽车

/**
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
 */
$parts

The only cases in which you need to specify the JoinColumns are: 您需要指定JoinColumns的唯一情况是:

  • Joining against self 反对自我
  • Joining where the primary key is not the Entity ID. 在主键不是实体ID的情况下加入。
  • Need to define fields in the join_table 需要在join_table中定义字段
    • Would be MUCH easier in this case to do A OneToMany J ManyToOne B 在这种情况下,做A OneToMany J ManyToOne B会容易A OneToMany J ManyToOne B

Since you're doing none of the above, Doctrine is having trouble accessing the Entities' IDENTITY fields for the join. 由于您没有执行上述任何操作,因此Doctrine无法访问用于连接的实体的IDENTITY字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM