I created a simple symfony2 script console that should convert data from the old model to the new one. Here's what it looks like:
class ConvertScreenshotsCommand extends Command { [...] protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get('doctrine')->getManager(); $output->writeln('<info>Conversion started on ' . date(DATE_RSS) . "</info>"); $output->writeln('Getting all reviews...'); $reviews = $em->getRepository('ACCommonBundle:Review')->findAll(); // Putting all Review entities into an array $output->writeln('<info>Got ' . count($reviews) . ' reviews.</info>'); foreach ($reviews as $review) { $output->writeln("<info>Screenshots for " . $review->getTitle() . "</info>"); if ($review->getLegacyScreenshots()) { foreach ($review->getLegacyScreenshots() as $filename) { // fn returns array of strings $output->writeln("Found " . $filename); $screenshot = new ReviewScreenshot(); // new object $screenshot->setReview($review); // review is object $screenshot->setFilename($filename); // filename is string $em->persist($screenshot); $em->flush(); // this is where it dies $output->writeln("Successfully added to the database."); } } else $output->writeln("No legacy screenshots found."); } $output->writeln('<info>Conversion ended on ' . date(DATE_RSS) . "</info>"); } }
the script breaks into $ em-> flush () with the following error:
[ErrorException] Warning: spl_object_hash() expects parameter 1 to be object, string given in /[...]/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1324
Obviously, I am doing something wrong, but I canβt understand what it is. Thanks in advance!
** Update **
Entity mapping display:
class Review { [...] protected $screenshots; private $legacyScreenshots; public function getScreenshots() {
Picture ReviewScreenshot:
class ReviewScreenshot { private $id; private $filename; protected $review; protected $priority; protected $description; public $screenshot_file; protected $webPath;
UnitOfWork.php
/** * Gets the state of an entity with regard to the current unit of work. * * @param object $entity * @param integer $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). * This parameter can be set to improve performance of entity state detection * by potentially avoiding a database lookup if the distinction between NEW and DETACHED * is either known or does not matter for the caller of the method. * @return int The entity state. */ public function getEntityState($entity, $assume = null) { $oid = spl_object_hash($entity); // <-- Line 1324 if (isset($this->entityStates[$oid])) { return $this->entityStates[$oid]; } if ($assume !== null) { return $assume; } // State can only be NEW or DETACHED, because MANAGED/REMOVED states are known. // Note that you can not remember the NEW or DETACHED state in _entityStates since // the UoW does not hold references to such objects and the object hash can be reused. // More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it. $class = $this->em->getClassMetadata(get_class($entity)); $id = $class->getIdentifierValues($entity); if ( ! $id) { return self::STATE_NEW; } switch (true) { case ($class->isIdentifierNatural()); // Check for a version field, if available, to avoid a db lookup. if ($class->isVersioned) { return ($class->getFieldValue($entity, $class->versionField)) ? self::STATE_DETACHED : self::STATE_NEW; } // Last try before db lookup: check the identity map. if ($this->tryGetById($id, $class->rootEntityName)) { return self::STATE_DETACHED; } // db lookup if ($this->getEntityPersister($class->name)->exists($entity)) { return self::STATE_DETACHED; } return self::STATE_NEW; case ( ! $class->idGenerator->isPostInsertGenerator()): // if we have a pre insert generator we can't be sure that having an id // really means that the entity exists. We have to verify this through // the last resort: a db lookup // Last try before db lookup: check the identity map. if ($this->tryGetById($id, $class->rootEntityName)) { return self::STATE_DETACHED; } // db lookup if ($this->getEntityPersister($class->name)->exists($entity)) { return self::STATE_DETACHED; } return self::STATE_NEW; default: return self::STATE_DETACHED; } }
source share