try { $record->save(); } catch(Doctrine_Exception $e) { if($e->getErrorCode() !== $duplicateKeyCode) { throw $e; } }
You must make sure that the same record does not exist on the application side, and not on the SQL side. If you never want the same article / tag combination to exist, add a unique index to (article_id, tag_id) . This should generate a mysql error, which in turn will throw a doctrine exception that you can catch. There is no ignoring the flag to save ... Perhaps you can use one that works at a lower DBAL level (Doctrine_Query, Doctrine_Connection, etc.), but not routed from the ORM level.
Doctrine_Record::isNew() will always return true if you created an instance record that was ready to pull it from db, otherwise it has a way to find out that record / is not new.
Also why do you use the MyISAM storage engine? I am sure that in fact this will lead to more overhead when using Doctrine, since then it needs to imitate the restrictions on the php side. Usually your circuit will 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
prodigitalson
source share