使用doctrine和mssql将数据插入表时出错

时间:2018-11-21 16:51:35

标签: php sql sql-server symfony doctrine-orm

我正在尝试通过这种方式(Symfony4)将数据插入具有相应表的已创建数据库中:

    /**
     * @Route("/admin/user/new", name="admin_add_new_user")
     */
    public function new_user(EntityManagerInterface $em)
    {
        $user = new User();
        $user->setUsername('felipito')
            ->setPassword('canelo123')
            ->setEmail('fpcanelo@ati.cu');

        $em->persist($user);
        $em->flush();

        return $this->render('admin/index.html.twig', [
            'controller_name' => 'AdminController',
            'brand' => 'brand',
            'msg' => sprintf(
                'New user dude: id #%d user %s', $user->getId(), $user->getUsername()
            )
        ]);
    }

一旦我打开网址,它就会引发以下内容:

An exception occurred while executing 'INSERT INTO user (username, roles, password, email) VALUES (?, ?, ?, ?)' with params ["felipito", "[]", "canelo123", "fpcanelo@ati.cu"]:

SQLSTATE [42000, 156]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'user'.
SQLSTATE [42000, 8180]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared.

问题可能与this problem I had yesterday

有关

2 个答案:

答案 0 :(得分:1)

就像@ rodmar-zavala回答的那样,问题是我在sql server中使用了保留字。这也适用于我在making the migration on doctrine时遇到的错误。

所以我重构了类名,它工作正常。

答案 1 :(得分:0)

看起来用户是MySQL中的保留字。 像这样更新您的用户实体@ORM \ Table注释。

 use Doctrine\ORM\Mapping as ORM;

 /**
 * @ORM\Entity
 * @ORM\Table(name="`user`")
 */
class User extends BaseUser
{
  // [....]
}

请注意,表名使用单引号。