教义:如何仅保存数据库中尚不存在的集合项

发布于 2024-08-18 12:21:03 字数 195 浏览 5 评论 0原文

我有一个项目集合要保存到数据库中,但我希望仅在不存在时才插入记录。

我认为最有效的方法是在保存之前过滤集合。 Doctrine能自动做到吗?

或者我应该获取集合中所有项目的所有 id,然后查询数据库以查找这些 id 列表中不存在的项目,然后在 foreach 中删除我们不需要的所有集合项目,最后保存集合?

有更好的方法建议吗?

I have a collection of items to save to database, but I want the record to be inserted only if not exists.

I think the most effective way would be to filter the collection before saving. Can Doctrine do it automatically?

Or shall I get all id's of all items in the collection, then query the database for items which do not exists on the list of these id's, then in foreach remove all collection items which we do not need, and finally save collection?

Any better approach suggested?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是偏爱你 2024-08-25 12:21:04

这是 Doctrine_Collection 类中的保存函数,

 public function save(Doctrine_Connection $conn = null, $processDiff = true)
    {
        if ($conn == null) {
            $conn = $this->_table->getConnection();
        }

        try {
            $conn->beginInternalTransaction();

            $conn->transaction->addCollection($this);

            if ($processDiff) {
                $this->processDiff();
            }

            foreach ($this->getData() as $key => $record) {
                $record->save($conn);
            }

            $conn->commit();
        } catch (Exception $e) {
            $conn->rollback();
            throw $e;
        }

        return $this;
    }

我不确定您从哪里获取集合,或者您是否手动构建它,但您可能想尝试扩展 Doctrine_Collection 类并重载保存函数,如下所示

<?php

class My_Collection extends Doctrine Collection
{

  public function save(Doctrine_Connection $conn = null, $processDiff = true, $createOnly = true)
    {
        if ($conn == null) {
            $conn = $this->_table->getConnection();
        }

        try {
            $conn->beginInternalTransaction();

            $conn->transaction->addCollection($this);

            if ($processDiff) {
                $this->processDiff();
            }

            foreach ($this->getData() as $key => $record) {

                if($createOnly)
                {
                    if ($record->exists())
                    {
                        $record->save($conn);
                    }
                }else{
                    $record->save($conn);
                }
            }

            $conn->commit();
        } catch (Exception $e) {
            $conn->rollback();
            throw $e;
        }

        return $this;
    }

}

This is the save function from the Doctrine_Collection class

 public function save(Doctrine_Connection $conn = null, $processDiff = true)
    {
        if ($conn == null) {
            $conn = $this->_table->getConnection();
        }

        try {
            $conn->beginInternalTransaction();

            $conn->transaction->addCollection($this);

            if ($processDiff) {
                $this->processDiff();
            }

            foreach ($this->getData() as $key => $record) {
                $record->save($conn);
            }

            $conn->commit();
        } catch (Exception $e) {
            $conn->rollback();
            throw $e;
        }

        return $this;
    }

I'm not sure where you are getting your collection from, or if you are manually building it, but you might want to try extending the Doctrine_Collection class and overloading the save function like this

<?php

class My_Collection extends Doctrine Collection
{

  public function save(Doctrine_Connection $conn = null, $processDiff = true, $createOnly = true)
    {
        if ($conn == null) {
            $conn = $this->_table->getConnection();
        }

        try {
            $conn->beginInternalTransaction();

            $conn->transaction->addCollection($this);

            if ($processDiff) {
                $this->processDiff();
            }

            foreach ($this->getData() as $key => $record) {

                if($createOnly)
                {
                    if ($record->exists())
                    {
                        $record->save($conn);
                    }
                }else{
                    $record->save($conn);
                }
            }

            $conn->commit();
        } catch (Exception $e) {
            $conn->rollback();
            throw $e;
        }

        return $this;
    }

}
中性美 2024-08-25 12:21:04

我对 Doctrine 一无所知,但是 MySQL REPLACE 查询正是您想要的 - 更新现有行和更新现有行。如果未找到主键匹配,则创建新行。

I don't know anything about Doctrine, but the MySQL REPLACE query does just what you want - updates existing rows & creates new rows if no primary key matches were found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文