插入symfony 3表

时间:2017-10-29 14:53:16

标签: php symfony insert symfony3.x

我需要在数据库中进行简单的插入,

以下是控制器中的代码:

    public function verifCreateOrder(Request $req){
        $store = 1;
        $provider =2;
        $creation =  $creation_start = $req->get('creation_start');
        $delivery = $creation_start = $req->get('creation_start');

        $em = $this->getDoctrine()->getManager();

        $order = new OrderList();
        $order->setNumStore($store);
        $order->setNumProvider($provider);
        $order->setCreation($creation);
        $order->setDelivery($delivery);

        $em->persist($order);
        $em->flush();
        die();
        return $this->redirectToRoute('search.view');
    }

OrderList实体的代码:

class OrderList
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\ManyToOne(targetEntity="Provider")
     * @ORM\JoinColumn(name="num_provider", referencedColumnName="id")
     */
    private $numProvider;

    /**
     * @var int
     *
     * @ORM\ManyToOne(targetEntity="Store")
     * @ORM\JoinColumn(name="num_store", referencedColumnName="id")
     */
    private $numStore;

    /**
     * @return int
     */
    public function getNumStore()
    {
        return $this->numStore;
    }

    /**
     * @param int $numStore
     */
    public function setNumStore($numStore)
    {
        $this->numStore = $numStore;
    }

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="creation", type="date")
     */
    private $creation;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="delivery", type="date")
     */
    private $delivery;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_update", type="date", nullable=true)
     */
    private $lastUpdate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="emission", type="date", nullable=true)
     */
    private $emission;

    /**
     * @var int
     *
     * @ORM\Column(name="reduction_1", type="integer", nullable=true)
     */
    private $reduction1;

    /**
     * @var int
     *
     * @ORM\Column(name="reduction_2", type="integer", nullable=true)
     */
    private $reduction2;

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

以下是symfony返回的错误:

  

关联字段“AppBundle \ Entity \ OrderList#$ numProvider”类型“AppBundle \ Entity \ Provider”的预期值,取而代之的是“整数”。

请帮我解决此错误。

2 个答案:

答案 0 :(得分:2)

您正在混合Doctrine relation和数据库foreign key概念(它们非常接近,但不相同)。问题是您无法将相关实体标识符视为Doctrine中的相关实体。您需要传递整个相关实体,而不仅仅是foreign key

至少有两种解决方案:

  1. 首先从数据库中检索此实体(store),而不是传递provider(1)的标识符和$em->find(...)(2)的标识符。
  2. 您可以使用getReference方法欺骗原则,该方法应该返回ID为$em->getReference('Path\To\Store', 1$em->getReference('Path\To\Provider', 2的所需对象的代理。
  3. 如果您想了解更多信息,可以查看以下文档:reference proxiesrelations

    public function verifyCreateOrder(Request $req){
        $storeId = 1;
        $providerId = 2;
        $creation =  $creation_start = $req->get('creation_start');
        $delivery = $creation_start = $req->get('creation_start');
    
        $em = $this->getDoctrine()->getManager();
        // getting references
        $store = $em->getReference(Store::class, $storeId);
        $provider = $em->getReference(Provider::class, $providerId);
    
        $order = new OrderList();
        $order->setNumStore($store);
        $order->setNumProvider($provider);
        $order->setCreation($creation);
        $order->setDelivery($delivery);
    
        $em->persist($order);
        $em->flush();
        die();
        return $this->redirectToRoute('search.view');
    }
    

答案 1 :(得分:1)

我也是php新手但是,你将$ provider设置为一个整数= 2,你的关系期望对象Provider $ provider。