1

Hello guys i have an error when i am creating a new "conversation" entity i really don't see where is the error :/ i have this :

An exception occurred while executing 'INSERT INTO conversation (read, last_send, id_user_one, id_user_two) VALUES (?, ?, ?, ?)' with params [0, 8, 8, 9]: 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 'read, last_send, id_user_one, id_user_two) VALUES (0, 8, 8, 9)' at line 1

My entity :

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id()
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

/**
 * @var \AppBundle\Entity\User
 *
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_user_one", referencedColumnName="id")
 * })
 */
protected $id_user_one;

/**
 * @var \AppBundle\Entity\User
 *
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_user_two", referencedColumnName="id")
 * })
 */
protected $id_user_two;

/**
 * @var int
 *
 * @ORM\Column(name="read", type="integer")
 */
protected $read;

/**
 * @var int
 *
 * @ORM\Column(name="last_send", type="integer")
 */
protected $last_send;

My controller :

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

$user = $em->getRepository(User::class)->findOneBy(array('id' => ($request->get('id_user'))));
$friend = $em->getRepository(User::class)->findOneBy(array('id' => ($request->get('id_friend'))));

$new_conv = new Conversation();
$new_conv->setLastSend($user->getId());
$new_conv->setRead(0);
$new_conv->setIdUserOne($user);
$new_conv->setIdUserTwo($friend);
$em->persist($new_conv);
$em->flush();
  • 5
    Read is a [reserved word](https://dev.mysql.com/doc/refman/5.7/en/keywords.html). You'll need to surround it with backticks. – aynber Oct 24 '18 at 13:06
  • ^^ That, or better yet, change the name. – Jonnix Oct 24 '18 at 13:06
  • 2
    Possible duplicate of [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – aynber Oct 24 '18 at 13:06
  • What name should i change the Entity "conversation" name ? –  Oct 24 '18 at 13:13
  • 2
    It depends on what the column is meant for. Number of times read? Use `times_read`. Just a boolean to mark that it's been read? `is_read` It's really up to you. – aynber Oct 24 '18 at 13:17
  • 1
    Ok thx for solve this :) You were right the problem was that 'read' is not acceptable –  Oct 24 '18 at 13:18
  • 1
    Another small mistake: only the $id needs GeneratedValue annotation. Not needed on the other ids. Rule of thumb: Only have it on Id() columns – mblaettermann Oct 24 '18 at 21:03

0 Answers0