我第一次使用Doctrine,它看起来很酷。但我不能将模型保存到数据库中。
目前我使用的是PHP 7.4和Doctrine 2.7.0。
我有一个简单的模型:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="place")
*/
class Place {
/**
* @ORM\Id
* @ORM\Column(type="integer", unique=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected int $id;
/** @ORM\Column(type="string", unique=true) */
private string $name;
/** @ORM\Column(type="integer") */
private int $ignore = 0;
/**
* @return int
*/
public function getId(): int {
return $this->id;
}
/**
* @return string
*/
public function getName(): string {
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void {
$this->name = $name;
}
/**
* @return int
*/
public function getIgnore(): int {
return $this->ignore;
}
/**
* @param int $ignore
*/
public function setIgnore(int $ignore): void {
$this->ignore = $ignore;
}
}表结构如下所示:

通过这个教程(https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/getting-started.html),我了解到我可以创建一个这样的新对象:
$p = new Place();
$p->setName("hello");并像这样将其保存到数据库中:
$entityManager = Doctrine::setup(); // sets up the entity manager
$entityManager->getConnection()
->getConfiguration()
->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
$entityManager->persist($p);
$entityManager->flush();但我得到以下SQL错误(包括调试信息):
"START TRANSACTION"
INSERT INTO place (name, ignore) VALUES (?, ?)
array(2) {
[1]=>
string(5) "hello"
[2]=>
int(0)
}
array(2) {
[1]=>
string(6) "string"
[2]=>
string(7) "integer"
}
"ROLLBACK"
Fatal error: Uncaught PDOException: 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 'ignore) VALUES ('hello', 0)' 我想,也许我做错了什么。我也尝试过其他模型,但我总是得到这个错误。
发布于 2020-01-24 21:10:31
这个问题可以用反引号来解决,请看这里:https://stackoverflow.com/a/43570215/832691
https://stackoverflow.com/questions/59893942
复制相似问题