title: Sonata Admin Image Field
body: <p>I am using the Sonata Admin back-end and I would like to add a <strong>new image field</strong> to my user entity which is an avatar. Since I am already using the <code>SonataMediaBundle</code> I followed this tutorial: <a href="https://sonata-project.org/blog/2013/10/11/mediabundle-mediatype-improved" rel="nofollow noreferrer">https://sonata-project.org/blog/2013/10/11/mediabundle-mediatype-improved</a></p>
  <p>Here is my entity configuration:</p>
  <pre><code>/**
   * @var \Application\Sonata\MediaBundle\Entity\Media
   *
   * @ORM\ManyToOne(targetEntity=&quot;Application\Sonata\MediaBundle\Entity\Media&quot;, cascade={&quot;all&quot;}, fetch=&quot;LAZY&quot;)
   * @ORM\JoinColumn(name=&quot;avatar_id&quot;, referencedColumnName=&quot;id&quot;)
   */
  protected $avatar;
  </code></pre>
  <p>Unfortunately I have many problems:</p>
  <ol>
  <li>In my back-end the preview is not shown:
  <a href="https://i.stack.imgur.com/5ms7j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5ms7j.png" alt="enter image description here" /></a></li>
  <li>If I delete the media in the gallery I receive this error when editing the user: <code>Entity of type 'Application\Sonata\MediaBundle\Entity\Media' for IDs id(6) was not found</code></li>
  <li>The resulting API (generated with FOSRestBundle) is unusable by the client:</li>
  </ol>
  <blockquote>
  <pre><code>&quot;avatar&quot;: {
          &quot;provider_metadata&quot;: {
              &quot;filename&quot;: &quot;Test.png&quot;
          },
          &quot;name&quot;: &quot;Test.png&quot;,
          &quot;description&quot;: null,
          &quot;enabled&quot;: false,
          &quot;provider_name&quot;: &quot;sonata.media.provider.image&quot;,
          &quot;provider_status&quot;: 1,
          &quot;provider_reference&quot;: &quot;325564b03489a6473e7c9def01dc58bab611eccb.png&quot;,
          &quot;width&quot;: 1430,
          &quot;height&quot;: 321,
          &quot;length&quot;: null,
          &quot;copyright&quot;: null,
          &quot;author_name&quot;: null,
          &quot;context&quot;: &quot;default&quot;,
          &quot;cdn_is_flushable&quot;: null,
          &quot;cdn_flush_at&quot;: null,
          &quot;cdn_status&quot;: null,
          &quot;updated_at&quot;: &quot;2017-08-08T12:31:19+02:00&quot;,
          &quot;created_at&quot;: &quot;2017-08-08T12:31:19+02:00&quot;,
          &quot;content_type&quot;: &quot;image/png&quot;,
          &quot;size&quot;: 24978,
          &quot;id&quot;: 7
      }
  </code></pre>
  </blockquote>