【问题标题】:Error in bidirectionnal relation双向关系错误
【发布时间】:2017-09-06 15:00:26
【问题描述】:

我正在做一个使用教义和 Symfony 的项目。 目前,当我尝试在具有双向关系的两个实体之间使用级联注释时遇到问题。

我有两个实体。

媒体索引器:

class MediaIndexer {
    /**
     * @var int
     * @ORM\Column(type="guid")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    private $id;

    /**
     * @var ArrayCollection $identifiers
     * @ORM\OneToMany(targetEntity="CelebrityBundle\Entity\MediaIdentifier",
     *     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;
    }
}

媒体标识符:

class MediaIdentifier {
    /**
     * @var int
     * @ORM\Column(type="guid")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    private $id;

    /**
     * @var MediaIndexer $indexer
     * @ORM\ManyToOne(targetEntity="CelebrityBundle\Entity\MediaIndexer", inversedBy="identifiers")
     * @ORM\JoinColumn(onDelete="CASCADE", referencedColumnName="id")
     */
    private $indexer;

    /**
     * @var string $content
     * @ORM\Column(name="key", type="string", length=255, nullable=false)
     */
    private $key;

    /**
     * @var int $type
     * @ORM\Column(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

你对这个问题有什么想法吗?

谢谢

【问题讨论】:

    标签: php sql symfony doctrine-orm orm


    【解决方案1】:

    KEY 是一个reserved word in MariaDB。尝试用backticks包围key的定义:

    /**
     * @var string $content
     * @ORM\Column(name="`key`", type="string", length=255, nullable=false)
     */
    private $key;
    

    【讨论】:

    • 天啊。我从星期一开始!非常感谢:p!
    • 没问题!发生在我们所有人身上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 2014-07-23
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多