title: symfony object could not be converted to string
body: <p>i'm newbie in symfony and I'm trying to implement a Data Transformer. I followed the documentation and looked different solutions posted in here. But I haven't found what is wrong with my code.</p>
  
  <p>I'm getting this error:</p>
  
  <p><strong>Catchable Fatal Error: Object of class AppBundle\Entity\Todo could not be converted to string</strong></p>
  
  <p>If someone knows another post with the solution, please tell me know where I could look for it. </p>
  
  <p>Thank you in advance.</p>
  
  <p>So, these are my Entities. Todo class</p>
  
  <pre><code>    &lt;?php
  
      namespace AppBundle\Entity;
  
      use Doctrine\ORM\Mapping as ORM;
  
      /**
       * Todo
       *
       * @ORM\Table(name="todo")
       * @ORM\Entity(repositoryClass="AppBundle\Repository\TodoRepository")
       */  
  
  
  class Todo
      {
          /**
           * @var int
           *
           * @ORM\Column(name="id", type="integer")
           * @ORM\Id
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          private $id;
  
          /**
           * @var string
           *
           * @ORM\Column(name="name", type="string", length=20)
           */
          private $name;
  
          /**
           * @var string
           *
           * @ORM\Column(name="category", type="string", length=20)
           */
          private $category;
  
          /**
           * @var string
           *
           * @ORM\Column(name="description", type="string", length=10)
           */
          private $description;
  
          /**
           * @var string
           *
           * @ORM\Column(name="priority", type="string", length=10)
           */
          private $priority;
  
          /**
           *
           * @ORM\ManyToOne(
           *     targetEntity="AppBundle\Entity\User",
           *     inversedBy="todos"
           * )
           * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
           */
          private $creatorId;
  
      //... getters and setters
  </code></pre>
  
  <p>User class:</p>
  
  <pre><code>&lt;?php
  // src/AppBundle/Entity/User.php
  
  namespace AppBundle\Entity;
  
  use FOS\UserBundle\Entity\User as BaseUser;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="fos_user")
   */
  class User extends BaseUser
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      public function __construct()
      {
          parent::__construct();
          // your own logic
      }
  
      /**
       * @ORM\OneToMany(
       *     targetEntity="AppBundle\Entity\Todo",
       *     mappedBy="creatorId"
       * )
       */
      private $todos;
  
  }
  </code></pre>
  
  <p>In the User class, I don't have getter/setters</p>
  
  <p>My TodoType</p>
  
  <pre><code>class TodoType extends AbstractType{
  
      private $manager;
  
      public function __construct(ObjectManager $manager)
      {
      $this-&gt;manager = $manager;
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
  
          $builder
  
              -&gt;add('name', TextType::class, array(
                  'label' =&gt; 'Name',
                  'required' =&gt; true
                )
              )
  
              -&gt;add('category', TextType::class, array(
                  'label' =&gt; 'Category'
                )
              )
  
              -&gt;add('priority', EntityType::class, array(
                  'class' =&gt; 'AppBundle:Todo',
                  'choice_label' =&gt; 'priority',
                )
              )
  
              -&gt;add('creatorId', TextType::class, array(
                'label' =&gt; 'creator Id:',
              ));
  
              $builder-&gt;get('creatorId')
                  -&gt;addModelTransformer(new IssueToNumberTransformer($this-&gt;manager));
  
      }
  
      public function getName()
      {
          return 'todo';
      }
  
  }
  </code></pre>
  
  <p>Transformer</p>
  
  <pre><code>&lt;?php
  namespace FOS\UserBundle\Form\DataTransformer;
  
  use AppBundle\Entity\User;
  use Doctrine\Common\Persistence\ObjectManager;
  use Symfony\Component\Form\DataTransformerInterface;
  use Symfony\Component\Form\Exception\TransformationFailedException;
  
  class IssueToNumberTransformer implements DataTransformerInterface
  {
      private $manager;
  
      public function __construct(ObjectManager $manager)
      {
          $this-&gt;manager = $manager;
      }
  
      /**
       * Transforms an object (creatorId) to a string (number).
       *
       * @param  creatorId|null $creatorId
       * @return string
       */
      public function transform($creatorId)
      {
          if (null === $creatorId) {
              return '';
          }
  
          return $creatorId-&gt;getId();
  
      }
  
      /**
       * Transforms a string (number) to an object (creatorId).
       *
       * @param  string $creatorId
       * @return creatorId|null
       * @throws TransformationFailedException if object (creatorId) is not found.
       */
      public function reverseTransform($creatorId)
      {
          // no issue number? It's optional, so that's ok
          if (!$creatorId) {
              return;
          }
  
          $creatorId = $this-&gt;manager
              -&gt;getRepository('AppBundle:User')
              // query for the issue with this id
              -&gt;find($creatorId);
  
  
          if (null === $creatorId) {
              throw new TransformationFailedException(sprintf(
                  'A user with number "%s" does not exist!',
                  $creatorId
              ));
          }
  
          return $creatorId;
      }
  }
  </code></pre>
  
  <p>Controller (function)</p>
  
  <pre><code>  public function createAction(Request $request){
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          // 1) build the form
          $todo = new Todo();
  
          $form = $this-&gt;createForm(new TodoType($em), $todo);
  
          // 2) handle the submit (will only happen on POST)
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
            // 3) save the Todo!
            $em-&gt;persist($todo);
            $em-&gt;flush();
  
          return $this-&gt;redirectToRoute('todo_create');
  
        }
  
          return $this-&gt;render('todo/create.html.twig', array(
              'todo'=&gt;$todo,
              'form'=&gt;$form-&gt;createView()
          ));
  
      }
  </code></pre>