Symfony2:实体问题

时间:2012-11-27 16:07:56

标签: mysql symfony doctrine-orm entity

我有两个实体templatediv他们的代码如下:(我是通过控制台自动生成的)

    <?php

namespace Tun\PublicBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Tun\PublicBundle\Entity\Template
 *
 * @ORM\Table(name="template")
 * @ORM\Entity
 */
class Template
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var integer $nbreDiv
     *
     * @ORM\Column(name="nbre_div", type="integer", nullable=false)
     */
    private $nbreDiv;



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

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

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

    /**
     * Set nbreDiv
     *
     * @param integer $nbreDiv
     */
    public function setNbreDiv($nbreDiv)
    {
        $this->nbreDiv = $nbreDiv;
    }

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

DIV:

<?php

namespace Tun\PublicBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Tun\PublicBundle\Entity\Div
 *
 * @ORM\Table(name="div")
 * @ORM\Entity
 */
class Div
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer $hauteur
     *
     * @ORM\Column(name="hauteur", type="integer", nullable=false)
     */
    private $hauteur;

    /**
     * @var integer $largeur
     *
     * @ORM\Column(name="largeur", type="integer", nullable=false)
     */
    private $largeur;

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


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

    /**
     * @var Template
     *
     * @ORM\ManyToOne(targetEntity="Template")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="template", referencedColumnName="id")
     * })
     */
    private $template;






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

    /**
     * Set hauteur
     *
     * @param integer $hauteur
     */
    public function setHauteur($hauteur)
    {
        $this->hauteur = $hauteur;
    }

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

    /**
     * Set largeur
     *
     * @param integer $largeur
     */
    public function setLargeur($largeur)
    {
        $this->largeur = $largeur;
    }

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


    /**
     * Set ind
     *
     * @param integer $ind
     */
    public function setInd($ind)
    {
        $this->ind = $ind;
    }

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


    /**
     * Set emplacement
     *
     * @param string $emplacement
     */
    public function setEmplacement($emplacement)
    {
        $this->emplacement = $emplacement;
    }

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

    /**
     * Set template
     *
     * @param Tun\PublicBundle\Entity\Template $template
     */
    public function setTemplate(\Tun\PublicBundle\Entity\Template $template)
    {
        $this->template = $template;
    }

    /**
     * Get template
     *
     * @return Tun\PublicBundle\Entity\Template 
     */
    public function getTemplate()
    {
        return $this->template;
    }
}

现在问题就在这里,我希望创建一个模板然后创建一个div,所以我继续这样做:

$template= new Template();
$template->setName($name);
$template->setNbreDiv($nbre_div);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($template);
$em->flush();

$d=new Div();
$d->setHauteur(100);
$d->setLargeur(100);
$d->setEmplacement("Gauche");
$d->setInd(1);
$d->setTemplate($template);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($d);
$em->flush();

我收到以下错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'div (hauteur, largeur, ind, emplacement, template) VALUES (100, 100, 1, 'Gauche'' at line 1

希望我把所有东西都放好,先谢谢你的帮助。我花了一整天的时间来尝试所有想到的组合......

1 个答案:

答案 0 :(得分:4)

DIV是MySQL中的保留关键字。它是一个运算符,用于整数除法。例如:

mysql> SELECT 35 DIV 7;
+----------+
| 35 DIV 7 |
+----------+
|        5 |
+----------+
1 row in set (0.02 sec)

我的建议是重命名这个实体。

参考:MySQL Arithmetic Operators