title: symfony doctrine using setters and getters dynamically
body: <p>I'm using symfony and doctrine. </p>
  
  <p>The server gets a HTTP PATCH request for the URL /company/{id} containing the property of a model and its value like <code>{"name": "My new name"}</code> The new value needs to be persisted into the DB.</p>
  
  <pre><code>$request = Request::createFromGlobals();
  $requestContentJSON = $request-&gt;getContent();
  $requestContentObj = json_decode($requestContentJSON);
  
  $repository = $this-&gt;getDoctrine()-&gt;getRepository('MyBundle:Company');
  $company = $repository-&gt;find($id);
  </code></pre>
  
  <p>Now I could just enter <code>$company-&gt;setName($requestContentObj[0]);</code> but the property being received will vary. Right now I'm using the following code to be able to handle every property:</p>
  
  <pre><code>foreach($requestContentObj as $key =&gt; $value){
      switch($key){
          case 'name':
              $company-&gt;setName($value);
              break;
          case 'department':
              $company-&gt;setDepartment($value);
              break;
          case 'origin':
              $company-&gt;setOrigin($value);
              break;
          case 'headquarters':
              $company-&gt;setHeadquarters($value);
              break;
          case 'email':
              $company-&gt;setEmail($value);
              break;
          case 'twitterid':
              $company-&gt;setTwitterId($value);
              break;
          case 'description':
              $company-&gt;setDescription($value);
              break;
      }
  }
  </code></pre>
  
  <p>But this doesn't look very smart especially because I know that I will have other entities like news, products, users, etc that will have their properties updated in the same manner. I'd like to do something like this:</p>
  
  <pre><code>$company-&gt;set("property", "value");
  </code></pre>
  
  <p>First thought that crossed my mind was to put this switch statement inside the company class inside this set function and also inside all the other entity classes I have. But is there a better way? Maybe symfony/doctrine has the solution already built-in, but I didn't find anything that would suit me.</p>
  
  <p>I still want to use setters and getters as a long-term investment.</p>
  
  <p>Thank you.</p>