我正在从事一个使用学说和符合的项目。目前,当我尝试使用具有双向关系的两个实体之间的级联注释时,我有一个问题。
我有两个实体。
MediaIndexer:
class MediaIndexer {
/**
* @var int
* @ORMColumn(type="guid")
* @ORMId
* @ORMGeneratedValue(strategy="UUID")
*/
private $id;
/**
* @var ArrayCollection $identifiers
* @ORMOneToMany(targetEntity="CelebrityBundleEntityMediaIdentifier",
* mappedBy="indexer",
* cascade={"all"},
* orphanRemoval=true)
*/
private $identifiers;
// ...
public function setValue($key, $value) {
foreach ($this->getIdentifiers() as $identifier) {
if ($identifier->getKey() == $key) {
$identifier->setValue($value);
return $this;
}
}
$identifier = new MediaIdentifier();
$identifier->setKey($key)
->setValue($value)
->setIndexer($this);
$this->addIdentifier($identifier);
return $this;
}
}
MediaIdentifier:
class MediaIdentifier {
/**
* @var int
* @ORMColumn(type="guid")
* @ORMId
* @ORMGeneratedValue(strategy="UUID")
*/
private $id;
/**
* @var MediaIndexer $indexer
* @ORMManyToOne(targetEntity="CelebrityBundleEntityMediaIndexer", inversedBy="identifiers")
* @ORMJoinColumn(onDelete="CASCADE", referencedColumnName="id")
*/
private $indexer;
/**
* @var string $content
* @ORMColumn(name="key", type="string", length=255, nullable=false)
*/
private $key;
/**
* @var int $type
* @ORMColumn(name="value", type="text", nullable=false)
*/
private $value;
// ...
}
(实际上,这些类用于在db中构造"关联数组")。
我尝试了一些小代码的关系:
public function testAction()
{
$indexer = new MediaIndexer();
$indexer->setValue("screenName", "katyperry");
$indexer->setValue("pageId", "00000");
$this->getDoctrine()->getManager()->persist($indexer);
$this->getDoctrine()->getManager()->flush();
}
,当我运行此代码时,我有错误:
An exception occurred while executing 'INSERT INTO medias_identifiers (id, key, value, indexer_id) VALUES (?, ?, ?, ?)' with params ["b8a4c4e3-9312-11e7-93c0-e55e28abe26b", "screenName", "katyperry", "b8a47f19-9312-11e7-93c0-e55e28abe26b"]:
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 'key, value, indexer_id) VALUES ('b8a4c4e3-9312-11e7-93c0-e55e28abe26b', 'screenN' at line 1
您对这个问题有任何了解吗?
谢谢
KEY
是Mariadb中的一个保留单词。尝试用反向围绕键的定义:
/**
* @var string $content
* @ORMColumn(name="`key`", type="string", length=255, nullable=false)
*/
private $key;