title: FOSRestBundle - Use ParamConverter to set current user
body: <p>Usually, On public APIs with multiple customers - all Entities should belong to a specific Entity (I.e Company, User etc.) - The owner of that Entity.</p>
  
  <hr>
  
  <h2>Example</h2>
  
  <p>Entity Resource:</p>
  
  <pre><code>/**
   * @ORM\Entity()
   * @ORM\Table(name="resource", uniqueConstraints={
   *     @ORM\UniqueConstraint(columns={"customer_id", "name"})
   * })
   * @UniqueEntity(fields={"customer", "name"}, groups={"save"})
   *
   * @JMS\ExclusionPolicy("all")
   */
  class Resource
  {
  
      /**
       * @ORM\Id
       * @ORM\GeneratedValue
       * @ORM\Column(type="integer")
       *
       * @JMS\Expose()
       * @JMS\Groups({"save_response"})
       * @JMS\Type(name="integer")
       */
      private $id;
  
      /**
       * @var Customer
       *
       * @ORM\ManyToOne(targetEntity="Customer", inversedBy="articles", cascade={"persist"})
       * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
       *
       * @JMS\Type("AppBundle\Entity\Customer")
       */
      private $customer;
  
  ...
  } 
  </code></pre>
  
  <p>Entity Customer:</p>
  
  <pre><code>/**
   * @ORM\Entity()
   * @ORM\Table(name="customer")
   *
   * @JMS\ExclusionPolicy("all")
   */
  class Customer
  {
      /**
       * @ORM\Id
       * @ORM\GeneratedValue
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @var ArrayCollection&lt;Resource&gt;
       *
       * @ORM\OneToMany(targetEntity="Resource", mappedBy="customer", cascade={"persist", "remove"})
       *
       * @JMS\Type("ArrayCollection&lt;AppBundle\Entity\Resource&gt;"))
       */
      private $resources;
  ..
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>class VariableController extends FOSRestController
  {
      /**
       * @Rest\Post(path="", name="resource_add")
       * @ParamConverter("resource", converter="fos_rest.request_body", options={"validator"={"groups"="save"}, "deserializationContext"={"groups"={"save"}}})
       * @param Resource $resource
       * @param ConstraintViolationList $validationErrors
       * @return View
       */
      public function addAction(Resource $resource ConstraintViolationList $validationErrors)
      {
          if (count($validationErrors)) { //Doesn't check for EntityUnique constraint - because its empty on request
              return $this-&gt;view($validationErrors, Response::HTTP_BAD_REQUEST);
          }
          $resource-&gt;setCustomer($this-&gt;getUser()-&gt;getCustomer()); //This should be done automatically
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($resource);
          $em-&gt;flush();
  
          return $this-&gt;view($resource, 200);
      }
  }
  </code></pre>
  
  <h2>Issue</h2>
  
  <p><code>Resource</code>'s <code>@UniqueEntity(fields={"customer", "name"}, groups={"save"})</code> is not used, simply because Customer relation is empty.</p>
  
  <hr>
  
  <h2>Question</h2>
  
  <p>Is there a way to set the logged-in user (or one of his relations) on a <code>Resource</code> using @ParamConverter?</p>
  
  <p>Please advise,
  Thanks!</p>