【问题标题】:Problem with symfony4/doctrine with oracle oci8 dbsymfony4/doctrine 与 oracle oci8 db 的问题
【发布时间】:2020-04-11 00:16:14
【问题描述】:

我在 Symfony 4 中使用教义/oracle/oci8 驱动程序向数据库插入新记录时遇到问题。 我的带有序列 ID 生成的实体配置:

<id name="id" type="integer" column="ID">
        <generator strategy="SEQUENCE"/>
        <sequence-generator sequence-name="FILES_SEQ" allocation-size="10" initial-value="1"/>
</id>

这就是我向数据库中插入新记录的方式:

$file = new File();
$file->setFileName($fileParams['fileName']);
...

$this->getEntityManager()->persist($file);
$this->getEntityManager()->flush();

记录已成功保存,但问题是当我尝试通过以下方式获取最后插入 ID 时:

$file->getId();

那么返回的 ID 比数据库中的 1 少 1。这是某种缓存问题吗?我尝试使用 getEntityManager()->refresh($file) 但它不起作用。 另外,当我调用 findAll() 方法时,那里的 ID 值是正确的。

【问题讨论】:

    标签: oracle symfony doctrine


    【解决方案1】:

    $file 是您发送到数据库的对象。在你的课堂上,我想你的File 课堂上有这样的东西:

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
    

    所以目标文件没有设置Id(因为它不是构造函数的一部分)。它是在您将其刷新到您的数据库时设置的。如果你想在你的逻辑中取回这个 ID,你需要从你的数据库中检索文件,比如:

    $this
        ->getEntityManager()
        ->getRepository(File::class)
        ->findOneByFileName($file->getFileName());
    

    【讨论】:

    • 我正在尝试在以下之后获取 ID:$this->getEntityManager()->flush();根据我的理解,这应该是可能的,因为记录已经保存在数据库中并且已经生成了 ID。而且它不是空的,它只是比数据库中的真实值小一。
    • 基本上我正在尝试做类似的事情:stackoverflow.com/questions/3509350/…
    • 并使用这个? $entityManager-&gt;getConnection()-&gt;lastInsertId()
    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2018-09-26
    • 2018-11-09
    相关资源
    最近更新 更多