Kevin Gravell Kevin Gravell - 2 days ago 3
PHP Question

Doctrine Error: syntax error or access violation: 10649

I have two classes Product and Manager, here is the code for each class (in Product I will put just related code):

Code for Product Entity:

<?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;
}


And here is the code for 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;
}
}


Into my controller I have a function where I am creating a Manager Entity and a Product, first I create a Manager object and later I create a Product object to link them. But there is a problem that I haven't see since yesterday when I try to insert the Manager object. I will paste here the error that gives me Symfony and the code where I obtain the error.

Code where I obtain the error:

$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();


And here is the error from 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


If someone could help me I will be very very grateful due to that I am really stuck with this problem... Thanks!

Answer

where is a reserved word. You should quote or rename your column. Try this (pay attention to backticks):

/**
* @ORM\Column(name="`where`", type="string", length=255, nullable=true)
*/
protected $where;
Comments