我有两个类Product和Manager,这里是每个类的代码(在Product中我将只提供相关代码):
产品实体代码:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="products")
* @ORM\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
*/
class Product {
/**
* @ORM\ManyToMany(targetEntity="Manager", mappedBy="products")
**/
private $manager;
}
这是Manager Entity的代码
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="shipping_methods")
* @ORM\Entity
*/
class Manager {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="decimal", precision=8, scale=2, options={"default" = 0})
*/
protected $percentage;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $name;
/**
* @ORM\Column(type="integer", nullable=false)
*/
protected $quantity;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $where;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $flag;
/**
* @ORM\ManyToMany(targetEntity="Product", inversedBy="shipping")
* @ORM\JoinTable(name="manager_methods")
**/
protected $products;
/**
* Constructor
*/
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set percentage
*
* @param string $percentage
* @return Manager
*/
public function setPercentage($percentage)
{
$this->percentage = $percentage;
return $this;
}
/**
* Get percentage
*
* @return string
*/
public function getPercentage()
{
return $this->percentage;
}
/**
* Set name
*
* @param string $name
* @return Manager
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set quantity
*
* @param integer $quantity
* @return Manager
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set where
*
* @param string $where
* @return Manager
*/
public function setWhere($where)
{
$this->where = $where;
return $this;
}
/**
* Get where
*
* @return string
*/
public function getWhere()
{
return $this->where;
}
/**
* Set flag
*
* @param string $flag
* @return Manager
*/
public function setFlag($flag)
{
$this->flag = $flag;
return $this;
}
/**
* Get flag
*
* @return string
*/
public function getFlag()
{
return $this->flag;
}
/**
* Add products
*
* @param \AppBundle\Entity\Product $products
* @return Manager
*/
public function addProduct(\AppBundle\Entity\Product $products)
{
$this->products[] = $products;
return $this;
}
/**
* Remove products
*
* @param \AppBundle\Entity\Product $products
*/
public function removeProduct(\AppBundle\Entity\Product $products)
{
$this->products->removeElement($products);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
在我的控制器中,我有一个函数,我在创建一个Manager实体和一个Product,首先我创建一个Manager对象,然后创建一个Product对象来链接它们。但是自从我昨天尝试插入Manager对象以来,我还没有看到一个问题。我将在此处粘贴给我Symfony的错误以及我获取错误的代码。
我获取错误的代码:
$em = $this->getDoctrine()->getManager();
$shipping = new Manager();
$shipping->setPercentage(1);
$shipping->setName("Name");
$shipping->setQuantity(1);
$shipping->setFlag("flag");
$shipping->setWhere("where");
$em->persist($shipping);
$em->flush();
以下是Symfony的错误:
An exception occurred while executing 'INSERT INTO shipping_methods (percentage, name, quantity, where, flag) VALUES (?, ?, ?, ?, ?)' with params [1, "Name", 1, "where", "flag"]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where, flag) VALUES ('1', 'Name', 1, 'where', 'flag')' at line 1
如果有人可以帮助我,我将非常感激,因为我真的被这个问题困住了......谢谢!
答案 0 :(得分:2)
where
是保留字。您应该quote或重命名您的专栏。
试试这个(注意反引号):
/**
* @ORM\Column(name="`where`", type="string", length=255, nullable=true)
*/
protected $where;