public function testIsValidForSaving_changeNameWithOwner() { $vasya = new User('vasya'); $ce1 = new ConfigurationEntry('cf_1'); $ce1->setValue('foo'); $ce2 = new ConfigurationEntry('cf_2'); $ce2->setValue('foo'); self::$em->persist($vasya); self::$em->persist($ce1); self::$em->persist($ce2); self::$em->flush(); $ce2->setName('cf_1'); $uv = new UniquityValidator(self::$em, array('owner_entity' => get_class($vasya))); $this->assertFalse($uv->isValidForSaving($ce2)); }
/** * @param ConfigurationEntryInterface $entry */ public function save(ConfigurationEntryInterface $entry) { if (!$entry instanceof ConfigurationEntry) { throw new InvalidArgumentException('$entry must be an instance of ' . ConfigurationEntry::clazz()); } $this->em->beginTransaction(); try { if ($this->uniquityValidator) { if (!$this->uniquityValidator->isValidForSaving($entry)) { throw new ConfigurationEntryAlreadyExistsException(sprintf('Configuration property with name "%s" already exists.', $entry->getName())); } } $this->em->persist($entry); $this->em->flush($entry); } catch (\Exception $e) { $this->em->rollback(); throw $e; } $this->em->commit(); }