title: Can't persist object in symfony2 console command
body: <p>I've made a simple symfony2 console script which is supposed to convert data from old model to the new one. 
  Here's what it looks like:</p>
  
  <pre><code>class ConvertScreenshotsCommand extends Command 
  {
  [...]
  protected function execute(InputInterface $input, OutputInterface $output)
  {
      $em = $this-&gt;getContainer()-&gt;get('doctrine')-&gt;getManager();
  
      $output-&gt;writeln('&lt;info&gt;Conversion started on ' . date(DATE_RSS) . "&lt;/info&gt;");
  
      $output-&gt;writeln('Getting all reviews...');
      $reviews = $em-&gt;getRepository('ACCommonBundle:Review')-&gt;findAll(); // Putting all Review entities into an array
      $output-&gt;writeln('&lt;info&gt;Got ' . count($reviews) . ' reviews.&lt;/info&gt;');
  
      foreach ($reviews as $review) {
          $output-&gt;writeln("&lt;info&gt;Screenshots for " . $review-&gt;getTitle() . "&lt;/info&gt;");
          if ($review-&gt;getLegacyScreenshots()) {
              foreach ($review-&gt;getLegacyScreenshots() as $filename) { // fn returns array of strings
                  $output-&gt;writeln("Found " . $filename);
                  $screenshot = new ReviewScreenshot();        // new object
  
                  $screenshot-&gt;setReview($review);     // review is object
                  $screenshot-&gt;setFilename($filename); // filename is string
  
                  $em-&gt;persist($screenshot);
                  $em-&gt;flush();                        // this is where it dies
                  $output-&gt;writeln("Successfully added to the database.");
              }
          } else $output-&gt;writeln("No legacy screenshots found.");
      }
      $output-&gt;writeln('&lt;info&gt;Conversion ended on ' . date(DATE_RSS) . "&lt;/info&gt;");
   }
   }
  </code></pre>
  
  <p>The script breaks on $em->flush(), with the following error:</p>
  
  <pre><code> [ErrorException]                                                                                                                                                       
   Warning: spl_object_hash() expects parameter 1 to be object, string given in      
   /[...]/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1324     
  </code></pre>
  
  <p>Obviously I'm doing something wrong, but can't figure out what it is. Thanks in advance!</p>
  
  <p>** Update ** </p>
  
  <p>Review Entity mapping:</p>
  
  <pre><code>class Review
  {
  [...]
  
  /**
   * @ORM\OneToMany(targetEntity="ReviewScreenshot", mappedBy="review")
   */
  protected $screenshots;
  
  /**
   * Won't be stored in the DB
   * @deprecated
   */
  private $legacyScreenshots;
  
  /**
   * New method to get screenshots, currently calls old method for the sake of compatibility
   * @return array Screenshot paths
   */
  
  public function getScreenshots()
  {
  //        return $this-&gt;getLegacyScreenshots();  // Old method
      return $this-&gt;screenshots;                   // New method
  }
  
  
  /**
   * Get Screenshot paths
   * @return array Screenshot paths
   * @deprecated
   */
  public function getLegacyScreenshots()
  {
      $dir=$this-&gt;getUploadRootDir();
      if (file_exists($dir)) {
          $fileList = scandir($dir);
  
      $this-&gt;screenshots = array();
      foreach ($fileList as $fileName)
      {
          preg_match("/(screenshot-\d+.*)/", $fileName, $matches);
          if ($matches)
              $this-&gt;screenshots[]=$matches[1];
      }
      return $this-&gt;screenshots;
      }
      else return null;
  }
  </code></pre>
  
  <p>ReviewScreenshot mapping:</p>
  
  <pre><code>class ReviewScreenshot
  {
  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string $filename
   *
   * @ORM\Column(name="filename", type="string", length=255)
   */
  private $filename;
  
  /**
   * @ORM\ManyToOne(targetEntity="Review", inversedBy="screenshots")
   * @ORM\JoinColumn(name="review_id", referencedColumnName="id")
   */
  protected $review;
  
  /**
   * @var integer $priority
   *
   * @ORM\Column(name="priority", type="integer", nullable=true)
   */
  protected $priority;
  
  /**
   * @var string $description
   *
   * @ORM\Column(name="description", type="string", nullable=true)
   */
  protected $description;
  
  /**
   * @Assert\File(maxSize="2097152")
   */
  public $screenshot_file;
  
  protected $webPath;
  </code></pre>
  
  <p>UnitOfWork.php</p>
  
  <pre><code>/**
   * 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); // &lt;-- Line 1324
  
      if (isset($this-&gt;entityStates[$oid])) {
          return $this-&gt;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-&gt;em-&gt;getClassMetadata(get_class($entity));
      $id    = $class-&gt;getIdentifierValues($entity);
  
      if ( ! $id) {
          return self::STATE_NEW;
      }
  
      switch (true) {
          case ($class-&gt;isIdentifierNatural());
              // Check for a version field, if available, to avoid a db lookup.
              if ($class-&gt;isVersioned) {
                  return ($class-&gt;getFieldValue($entity, $class-&gt;versionField))
                      ? self::STATE_DETACHED
                      : self::STATE_NEW;
              }
  
              // Last try before db lookup: check the identity map.
              if ($this-&gt;tryGetById($id, $class-&gt;rootEntityName)) {
                  return self::STATE_DETACHED;
              }
  
              // db lookup
              if ($this-&gt;getEntityPersister($class-&gt;name)-&gt;exists($entity)) {
                  return self::STATE_DETACHED;
              }
  
              return self::STATE_NEW;
  
          case ( ! $class-&gt;idGenerator-&gt;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-&gt;tryGetById($id, $class-&gt;rootEntityName)) {
                  return self::STATE_DETACHED;
              }
  
              // db lookup
              if ($this-&gt;getEntityPersister($class-&gt;name)-&gt;exists($entity)) {
                  return self::STATE_DETACHED;
              }
  
              return self::STATE_NEW;
  
          default:
              return self::STATE_DETACHED;
      }
  }
  </code></pre>