我想在我的数据库中写下交易。因此,我有一个TransactionEntity,其字段为'initiator'。该字段应该是用户的userId,负责事务。为了实现这一点,我在TransactionEntity中添加了ManyToOne关系,但每次尝试插入新对象时,都会出现SQL错误。
这是我的用户实体类:
/**
* @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
}
然后我有我的交易实体类:
/**
* @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: "$2y$13$h7oYgYPS5aN.kJBk7ve8jO.AmQJSKhcvT0qNhPO2Lk6p88c2.j1xG"
-displayname: "Benutzer"
-roles: array:1 [▶]
-isActive: true
-isVisible: true
}
}
但是当我尝试持久保存此对象然后尝试刷新它时,它会抛出此错误:
未捕获的PHP异常Doctrine \ DBAL \ Exception \ SyntaxErrorException: “执行'INSERT INTO时发生异常 abistuff_transactions(value,usage,date,user_id)VALUES(?,?,?, ?)'with params [10,“Cookies”,“2017-10-23”,2]:SQLSTATE [42000]: 语法错误或访问冲突:1064 SQL中有错误 句法;检查与MariaDB服务器对应的手册 在'usage,date,user_id)VALUES附近使用正确语法的版本 (10,'Cookies','2017-10-23',2)'在第1行“at /home/fatih/PhpstormProjects/Abistuff-Symfony4/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php 第90行
有人能帮助我吗?
编辑:我不知道“使用”是保留的。答案 0 :(得分:0)
这是因为usage
是reserved word你不能使用它。更改字段的名称,如:
/**
* @ORM\Column(type="text", name="transaction_usage")
* @Assert\NotBlank()
*/
private $usage;
或逃避它:
/**
* @ORM\Column(type="text", name="`usage`")
* @Assert\NotBlank()
*/
private $usage;