Reputation: 2476
I have looking around for too long with no luck. My situation is that i have a bit large table +60 columns which is represented in Doctrine Entity. Working on FosREST and what i want to achieve is that i want to send a JSON with specific data let's say for example
[phone] => new_phone
[name] => new_name
[id] => 1
while like i said the entity contains over 60 columns like address, picture, category, etc...
and the phone, name and id are not what I want to change every time but i want to change some columns each time. So at some time i might want to update phone and name other time i want to change the category third time i want to change category and photo and address so is there anything like this ?
$entity->update($parameters);
where the $parameters are dynamically changed as explained before. ps. i know that i can build a very long function with something like
if(isset($parameters['name']){
$entity->setName($parameters['name']);
}
but with 60 ifs this just sounds idiotic anyone have any other approach ? Thanks
Upvotes: 0
Views: 3112
Reputation: 1497
1) If the parameters are named after the attributes (here with underscore annotations), you can do this
use Doctrine\Common\Util\Inflector;
// ...
public function setParameters($params) {
foreach ($params as $k => $p) {
$key = Inflector::camelize($k);
if (property_exists($this, $key)) {
$this->$key = $p;
}
}
return $this;
}
2) Same thing with the setters
use Doctrine\Common\Util\Inflector;
// ...
public function setParameters($params) {
foreach ($params as $k => $p) {
$key = Inflector::camelize($k);
if (property_exists($this, $key)) {
$this->{'set'.ucfirst($key)}($p); // ucfirst() is not required but I think it's cleaner
}
}
return $this;
}
3) If its not the same name, you can do this :
public function setParameters($params) {
foreach ($params as $k => $p) {
switch $k {
case 'phone':
$this->phoneNumber = $p;
break;
// ...
}
}
return $this;
}
EDIT : Best approach is number two but you should define a white-list or a black-list in order to avoid the user updating something you don't want him to.
Upvotes: 5