Symfony ManyToOne 关系 SQL 错误
Symfony ManyToOne Relation SQL Error
我正在尝试在我的数据库中记下交易记录。因此我有一个 TransactionEntity,它有字段 'initiator'。该字段应该是负责交易的用户的userId。为实现此目的,我向 TransactionEntity 添加了 ManyToOne 关系,但每次尝试插入新对象时,我都会收到 SQL 错误。
这是我的用户实体class:
/**
* @ORM\Table(name="abistuff_users")
* @ORM\Entity()
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string", length=60)
* @Assert\NotBlank()
*/
private $displayname;
/**
* @ORM\Column(type="json_array")
* @Assert\NotBlank()
*/
private $roles;
/**
* @ORM\Column(name="is_active", type="boolean")
* @Assert\NotBlank()
*/
private $isActive;
/**
* @ORM\Column(name="is_visible", type="boolean")
* @Assert\NotBlank()
*/
private $isVisible;
//Getters, Setters, and more stuff required for login
}
然后我有我的交易实体class:
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
*/
private $value;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
private $usage;
/**
* @ORM\Column(type="date")
* @Assert\NotBlank()
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $initiator;
这是我创建表单的函数,我尝试插入我的对象。
public function showAction(Request $request)
{
$form = $this->createFormBuilder()
->setMethod("POST")
->add('value', NumberType::class, array(
'label' => 'Wert',
'attr' => array('placeholder' => 'z.B. "10" oder "-5"', 'autofocus' => ''),
'scale' => 2
))
->add('usage', TextareaType::class, array(
'label' => 'Verwendungszweck',
'attr' => array('placeholder' => 'Ich musste ganz dringend ganz viele Kekse kaufen...')
))
->add('date', DateType::class, array(
'label' => 'Datum',
'widget' => 'single_text',
'format' => 'dd.MM.yyyy',
'attr' => array('id' => 'form_date')
))
->add('initiator', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('u')
->where('u.isVisible = 1')
->orderBy('u.displayname', 'ASC');
},
'choice_label' => 'displayname',
'label' => 'Wer ist dafür verantwortlich?'
))
->add('submit', SubmitType::class, array(
'label' => 'Transaktion hinzufügen',
'attr' => array('class' => 'btn-success')
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//Daten aus der Form holen
$data = $form->getData();
//Transaction-Objekt bauen
$transaction = new BankTransaction();
$transaction->setValue($data['value']);
$transaction->setUsage($data['usage']);
$transaction->setDate($data['date']);
$transaction->setInitiator($data['initiator']);
//dump($transaction);
//die();
//In die DB schreiben
$em = $this->getDoctrine()->getManager();
$em->persist($transaction);
$em->flush();
$this->addFlash('success', 'Neue Transaktion über ' . $transaction->getValue() . ' wurde notiert!');
}
return $this->render('/abistuff/user/bank/newtransaction.html.twig', array('form' => $form->createView()));
}
这是我尝试写入数据库的对象的转储:
BankTransaction {#1145 ▼
-id: null
-value: 10.0
-usage: "Cookies"
-date: DateTime {#680 ▼
+"date": "2017-10-23 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
-initiator: User {#805 ▼
-id: 2
-username: "user"
-password: "y$h7oYgYPS5aN.kJBk7ve8jO.AmQJSKhcvT0qNhPO2Lk6p88c2.j1xG"
-displayname: "Benutzer"
-roles: array:1 [▶]
-isActive: true
-isVisible: true
}
}
但是当我尝试保留这个对象然后尝试刷新它时,它会抛出这个错误:
Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException:
"An exception occurred while executing 'INSERT INTO
abistuff_transactions (value, usage, date, user_id) VALUES (?, ?, ?,
?)' with params [10, "Cookies", "2017-10-23", 2]: 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 'usage, date, user_id) VALUES
(10, 'Cookies', '2017-10-23', 2)' at line 1" at
/home/fatih/PhpstormProjects/Abistuff-Symfony4/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
line 90
有人可以帮我吗?
编辑:我不知道 "usage" 被保留了。
因为usage
是一个reserved word你不能用。更改字段名称,例如:
/**
* @ORM\Column(type="text", name="transaction_usage")
* @Assert\NotBlank()
*/
private $usage;
或者逃避它:
/**
* @ORM\Column(type="text", name="`usage`")
* @Assert\NotBlank()
*/
private $usage;
我正在尝试在我的数据库中记下交易记录。因此我有一个 TransactionEntity,它有字段 'initiator'。该字段应该是负责交易的用户的userId。为实现此目的,我向 TransactionEntity 添加了 ManyToOne 关系,但每次尝试插入新对象时,我都会收到 SQL 错误。
这是我的用户实体class:
/**
* @ORM\Table(name="abistuff_users")
* @ORM\Entity()
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string", length=60)
* @Assert\NotBlank()
*/
private $displayname;
/**
* @ORM\Column(type="json_array")
* @Assert\NotBlank()
*/
private $roles;
/**
* @ORM\Column(name="is_active", type="boolean")
* @Assert\NotBlank()
*/
private $isActive;
/**
* @ORM\Column(name="is_visible", type="boolean")
* @Assert\NotBlank()
*/
private $isVisible;
//Getters, Setters, and more stuff required for login
}
然后我有我的交易实体class:
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
*/
private $value;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
private $usage;
/**
* @ORM\Column(type="date")
* @Assert\NotBlank()
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $initiator;
这是我创建表单的函数,我尝试插入我的对象。
public function showAction(Request $request)
{
$form = $this->createFormBuilder()
->setMethod("POST")
->add('value', NumberType::class, array(
'label' => 'Wert',
'attr' => array('placeholder' => 'z.B. "10" oder "-5"', 'autofocus' => ''),
'scale' => 2
))
->add('usage', TextareaType::class, array(
'label' => 'Verwendungszweck',
'attr' => array('placeholder' => 'Ich musste ganz dringend ganz viele Kekse kaufen...')
))
->add('date', DateType::class, array(
'label' => 'Datum',
'widget' => 'single_text',
'format' => 'dd.MM.yyyy',
'attr' => array('id' => 'form_date')
))
->add('initiator', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('u')
->where('u.isVisible = 1')
->orderBy('u.displayname', 'ASC');
},
'choice_label' => 'displayname',
'label' => 'Wer ist dafür verantwortlich?'
))
->add('submit', SubmitType::class, array(
'label' => 'Transaktion hinzufügen',
'attr' => array('class' => 'btn-success')
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//Daten aus der Form holen
$data = $form->getData();
//Transaction-Objekt bauen
$transaction = new BankTransaction();
$transaction->setValue($data['value']);
$transaction->setUsage($data['usage']);
$transaction->setDate($data['date']);
$transaction->setInitiator($data['initiator']);
//dump($transaction);
//die();
//In die DB schreiben
$em = $this->getDoctrine()->getManager();
$em->persist($transaction);
$em->flush();
$this->addFlash('success', 'Neue Transaktion über ' . $transaction->getValue() . ' wurde notiert!');
}
return $this->render('/abistuff/user/bank/newtransaction.html.twig', array('form' => $form->createView()));
}
这是我尝试写入数据库的对象的转储:
BankTransaction {#1145 ▼
-id: null
-value: 10.0
-usage: "Cookies"
-date: DateTime {#680 ▼
+"date": "2017-10-23 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
-initiator: User {#805 ▼
-id: 2
-username: "user"
-password: "y$h7oYgYPS5aN.kJBk7ve8jO.AmQJSKhcvT0qNhPO2Lk6p88c2.j1xG"
-displayname: "Benutzer"
-roles: array:1 [▶]
-isActive: true
-isVisible: true
}
}
但是当我尝试保留这个对象然后尝试刷新它时,它会抛出这个错误:
Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException: "An exception occurred while executing 'INSERT INTO abistuff_transactions (value, usage, date, user_id) VALUES (?, ?, ?, ?)' with params [10, "Cookies", "2017-10-23", 2]: 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 'usage, date, user_id) VALUES (10, 'Cookies', '2017-10-23', 2)' at line 1" at /home/fatih/PhpstormProjects/Abistuff-Symfony4/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 90
有人可以帮我吗?
编辑:我不知道 "usage" 被保留了。
因为usage
是一个reserved word你不能用。更改字段名称,例如:
/**
* @ORM\Column(type="text", name="transaction_usage")
* @Assert\NotBlank()
*/
private $usage;
或者逃避它:
/**
* @ORM\Column(type="text", name="`usage`")
* @Assert\NotBlank()
*/
private $usage;