title: Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting
body: <p>A symfony2 application has a <code>Job</code> entity that has as a property of type <code>WebSite</code>.</p>
  
  <p>A simplified representation of this without other properties or methods:</p>
  
  <pre><code>/**
   * @ORM\Entity
   * @ORM\Table(name="Job")
   */
  class Job
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
  
      /**
       * @ORM\Column(type="integer", name="website_id", nullable=false)
       * @ORM\ManyToOne(targetEntity="Example\ExampleBundle\Entity\WebSite")
       */
      protected $website;
  }
  </code></pre>
  
  <p>Symfony/Doctrine is trying to cast the <code>website</code> property to a string when persisting resulting in the error:</p>
  
  <blockquote>
    <p>Catchable Fatal Error: Object of class
    Example\ExampleBundle\Entity\WebSite could not be converted to string
    in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 131</p>
  </blockquote>
  
  <p>I believe the above @ORM\Column annotation denotes the <code>website</code> property to be an integer. I don't understand why Symfony/Doctrine wishes to try and convert the <code>website</code> property to a string.</p>
  
  <p>Non-ideal workarounds I have tried in an attempt to resolve the matter:</p>
  
  <ul>
  <li><p>Adding <code>__toString()</code> method to <code>WebSite</code>, returning a string representation of the <code>id</code> property, causes the correct value to ultimately end up in the <code>Job.website_id</code> datafield; this workaround is impractical as I will in the future need <code>__toString()</code> to be available to present a string elsewhere in the application.</p></li>
  <li><p>Removing the <code>@ORM\Column</code> annotation from the <code>website</code> property; this results in all future diff-generated migrations (php app/console doctrine:migrations:diff) removing the 'not null' aspect of the relevant database field.</p></li>
  </ul>
  
  <p>Are there any changes I should make to the above annotation to inform Symfony/Doctrine to call the <code>getId()</code> method of <code>WebSite</code> to get what it needs when persisting? Are there any changes I could make to <code>WebSite</code> to achieve the same?</p>
  
  <p>What changes are required to ensure that the above annotation can remain in place such that Doctrine ultimately calls <code>WebSite.getId()</code> to get what it needs when persisting instead of trying to cast the object to a string?</p>