将当前日期插入数据库

时间:2015-05-19 12:46:36

标签: php mysql symfony doctrine-orm

我收到此错误因为我将DateTime $ date变量添加到实体我无法插入当前日期或向数据库添加修复。我有这个错误:

INSERT INTO修复(expDate,repaires,currency,price,date,cars_id,garages_id)VALUES(?,?,?,?,?,?,?)'with params [“2016-07-12”,“ window“,”$“,400,null,1,5]

我在互联网上搜索了3个小时但没有得到任何结果。 请帮助。

<?php

    namespace OBCarsTest2Bundle\Controller;


    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Validator\Constraints\DateTime;
    use OBCarsTest2Bundle\Entity\repaires;
    use OBCarsTest2Bundle\Entity\garages;
    use OBCarsTest2Bundle\Entity\cars;
    use OBCarsTest2Bundle\Entity\garagesBrands;
    use OBCarsTest2Bundle\Entity\brands;
    use Doctrine\ORM\EntityRepository;

    class RepairesController extends Controller
    {
        public function addRepairesAction($vin,$repair,$garage,$expDate,$price,$currency)//function to insert the repairs in the database
        {
            $addRepaires = new repaires();
            //$addBrandToGarage = new garagesBrands();

            $CarToRepaire = $this->getDoctrine()->getManager()->getRepository('OBCarsTest2Bundle:cars')->findOneByvin($vin);
            $GarageOfRepaire = $this->getDoctrine()->getManager()->getRepository('OBCarsTest2Bundle:garages')->findOneByname($garage);
            //$GarageBrand = $this->getDoctrine()->getManager()->getRepository('OBCarsTest2Bundle:garagesBrands')->findByidGarage($GarageOfRepaire);

            $idBrand = $CarToRepaire->getBrands();
            $CheckCarBrand = $this->getDoctrine()->getManager()->getRepository('OBCarsTest2Bundle:brands')->findOneById($idBrand);

            //to compare the dates=> expiry dates
            $DateExp = date_create($expDate);

            $Date = date("Y-m-d");
            $Today = date_create($Date);

            $diff=date_diff($Today,$DateExp);
            $limite = $diff->format("%R%a days");

            if($CarToRepaire != Null && $GarageOfRepaire != Null && 0<$limite)
            {
                //to insert the repaires of the users
                $addRepaires->setPrice($price)
                            ->setRepaires($repair)
                            ->setIdGarages($GarageOfRepaire)
                            ->setIdCars($CarToRepaire)
                            ->setCurrency($currency)
                            ->setExpDate($DateExp)
                            ->setDate(date("Y-m-d"));

                $em1 = $this->getDoctrine()->getEntityManager();
                $em1->persist($addRepaires);
                $em1->flush();
                }
          }
    }

这是实体:

<?php

namespace OBCarsTest2Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * repaires
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="OBCarsTest2Bundle\Entity\repairesRepository")
 */
class repaires
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="price", type="bigint")
     */
    private $price;

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

    /**
     * @var string
     * @ORM\ManyToOne(targetEntity="cars", inversedBy="repaires")
     * @ORM\JoinColumn(name="cars_id", referencedColumnName="id")
     **/
    private $idCars;

    /**
     * @var string
     * @ORM\ManyToOne(targetEntity="garages", inversedBy="repaires")
     * @ORM\JoinColumn(name="garages_id", referencedColumnName="id")
     **/
    private $idGarages;


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

    /**
     * Set expDate
     *
     * @param \DateTime $expDate
     * @return repaires
     */
    public function setExpDate($expDate)
    {
        $this->expDate = $expDate;

        return $this;
    }

    /**
     * Get expDate
     *
     * @return \DateTime 
     */
    public function getExpDate()
    {
        return $this->expDate;
    }

    /**
     * Set repaires
     *
     * @param string $repaires
     * @return repaires
     */
    public function setRepaires($repaires)
    {
        $this->repaires = $repaires;

        return $this;
    }

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

    /**
     * Set currency
     *
     * @param string $currency
     * @return repaires
     */
    public function setCurrency($currency)
    {
        $this->currency = $currency;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set idGarages
     *
     * @param integer $idGarages
     * @return repaires
     **/
    public function setIdGarages($idGarages)
    {
        $this->idGarages = $idGarages;

        return $this;
    }

    /**
     * Get idGarages
     *
     * @return integer 
     **/
    public function getIdGarages()
    {
        return $this->idGarages;
    }

    /**
     * Set idCars
     *
     * @param integer $idCars
     * @return repaires
     **/
    public function setIdCars($idCars)
    {
        $this->idCars = $idCars;

        return $this;
    }

    /**
     * Get idCars
     *
     * @return integer 
     **/
    public function getIdCars()
    {
        return $this->idCars;
    }

    /**
     * Set Date
     *
     * @param \DateTime $date
     * @return repaires
     */
    public function setDate($date)
    {
        $this->Date = $date;

        return $this;
    }

    /**
     * Get Date
     *
     * @return \DateTime 
     */
    public function getDate()
    {
        return $this->Date;
    }
}

1 个答案:

答案 0 :(得分:0)

setDate和getDate中的实体var名称错误。将$ this-&gt;日期更改为$ this-&gt; date。