使用Symfony和Doctrine的数据库中使用INSERT时

使用Symfony和Doctrine的数据库中使用INSERT时

本文介绍了在使用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将INSERT IGNORE插入表?

How to do INSERT IGNORE into table with symfony?

$sbTagsArticles->isNew();

返回1。

Thnx。 / p>

Thnx.

推荐答案

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错误,这将反过来产生一个你可以捕获的doctrine异常。没有一个忽略标志的保存...您可能可以使用一个操作在较低级别的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,如果您将实例化记录反对从数据库中拉它,否则它有办法没有办法知道记录是/ isnt新的。

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时,忽略重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:05