本文介绍了在带有 Symfony 和 Doctrine 的数据库中使用 INSERT 时忽略重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子

CREATE TABLE `sob_tags_articles` (
  `tag_id` int(11) NOT NULL,
  `article_id` int(11) NOT NULL,
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=112

并尝试使用 Doctrine 保存对象:

And triing to save an object with Doctrine:

$sbTagsArticles = new SobTagsArticles();
$sbTagsArticles->article_id = $pubId;
$sbTagsArticles->tag_id = $tagId;
$sbTagsArticles->save();

但是如果存在具有相同 $pubId 和 $tagId 的记录,则新记录将插入新的 PK.

But if record exists with the same $pubId and $tagId new record will be insertet with new PK.

如何使用 symfony 将 IGNORE 插入到表中?

How to do INSERT IGNORE into table with symfony?

$sbTagsArticles->isNew();

返回 1.

谢谢.

推荐答案

try
{
    $record->save();
}
catch(Doctrine_Exception $e)
{
    if($e->getErrorCode() !== $duplicateKeyCode)
    {
        /**
         * if its not the error code for a duplicate key
         * value then rethrow the exception
         */
        throw $e;
    }

    /**
     * you might want to fetch the real record here instead
     * so yure working with the persisted copy
     */
}

您应该确保在应用程序端而不是 SQL 端不存在相同的记录.如果您不希望存在相同的文章/标签组合,则将唯一索引添加到 (article_id, tag_id).这应该会生成一个 mysql 错误,进而生成一个您可以捕获的学说异常.没有用于保存的忽略标志...您可以使用在 DBAL 的较低级别(Doctrine_Query、Doctrine_Connection 等)操作的标志,但不能直接从 ORM 层使用.

You should be ensuring that the same record doesnt exist on the application side not the SQL side. If you dont ever want the same article/tag combo to exist then add a unique index to (article_id, tag_id). That should generate a mysql error which will in turn generate a doctrine exception that you can catch. There isnt an ignore flag for saves... You might be able to use one operating at a lower level of the DBAL (Doctrine_Query, Doctrine_Connection, etc..) but not directl from the ORM layer.

Doctrine_Record::isNew() 如果您已实例化记录而不是将其从数据库中提取,则将始终返回 true,否则它无法知道该记录是/不是新的.

Doctrine_Record::isNew() will always return true if you have instantiated record asopposed to pulling it from the db otherwise it has way it has no way to know that the record is/isnt new.

另外,您为什么使用 MyISAM 存储引擎?我很确定这实际上会在使用 Doctrine 时导致更多开销,因为它需要在 php 端模拟约束.通常你的架构看起来像这样:

Also why are you using the MyISAM storage engine? Im pretty sure this will actually result in more overhead when using Doctrine since it then needs to emulate constraints on the php side. Normally your schema would look something like this:

CREATE TABLE `sob_tags_articles` (
  `tag_id` int(11) NOT NULL,
  `article_id` int(11) NOT NULL,
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`),
  CONSTRAINT `some_unique_constraint_name_1`
      FOREIGN KEY `article_id`
      REFERENCES `article` (`id`)
      ON DELETE CASCADE,
  CONSTRAINT `some_unique_constraint_name_2`
      FOREIGN KEY `tag_id`
      REFERENCES `tag` (`id`)
      ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=112

这篇关于在带有 Symfony 和 Doctrine 的数据库中使用 INSERT 时忽略重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 19:22