Symfony doctrine that uses setters and getters dynamically

I use symfony and doctrine.

The server receives an HTTP PATCH request for the URL / company / {id} containing the model property and its value, similar {"name": "My new name"}. The new value must be saved in the database.

$request = Request::createFromGlobals();
$requestContentJSON = $request->getContent();
$requestContentObj = json_decode($requestContentJSON);

$repository = $this->getDoctrine()->getRepository('MyBundle:Company');
$company = $repository->find($id);

Now I can just enter $company->setName($requestContentObj[0]);, but the resulting property will be different. Right now I am using the following code to be able to handle each property:

foreach($requestContentObj as $key => $value){
    switch($key){
        case 'name':
            $company->setName($value);
            break;
        case 'department':
            $company->setDepartment($value);
            break;
        case 'origin':
            $company->setOrigin($value);
            break;
        case 'headquarters':
            $company->setHeadquarters($value);
            break;
        case 'email':
            $company->setEmail($value);
            break;
        case 'twitterid':
            $company->setTwitterId($value);
            break;
        case 'description':
            $company->setDescription($value);
            break;
    }
}

But this does not look very smart, especially because I know that I will have other objects, such as news, products, users, etc. that will update their properties in the same way. I would like to do something like this:

$company->set("property", "value");

, switch , , . ? , symfony/doctrine , , .

.

.

+4
3

, , .

- . .

Class customer {

    protected $_email;

    public function __construct(array $config = array()){
         $this->setOptions($config);
     }

    public function getEmail(){
        return $this->_email;
    }

    public function setEmail($email){
        $this->_email = $email;
    }

    public function setOptions(array $options)
    {
        $_classMethods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $_classMethods)) {
                $this->$method($value);
            } else {
                throw new Exception('Invalid method name');
            }
        }
        return $this;
    }

    public function setOption($key, $value){
        return $this->setOptions(array($key, $value));
    }

}

:

$array = array('email' => 'abc.@gmail.com');
$customer = new Customer($array);
echo $customer->getEmail();
+9

, merge, :

<?php

// example Company entity
class Company
{
    private $name;

    function setName($name)
    {
        $this->name = $name;
    }

    function getName()
    {
        return $this->name;
    }

    function merge(\stdClass $obj)
    {
        // get the object vars of the passed object
        // iterate, and replace matching properties
        foreach (get_object_vars($obj) as $prop => $val) {
            if (property_exists($this, $prop)) {
                $this->$prop = $val;
            }
        }
    }
}

$company = new Company();

// mocking your request object
$requestContentObj = new stdClass();
$requestContentObj->name = 'acme';

$company->merge($requestContentObj);

var_dump($company);

:

class Company#1 (1) {
    private $name =>
    string(4) "acme"
}

, - Company, , . , :)

+4

What I can offer does not use setters, but it seems like this is well suited to your problem. In doctrine 1.2.4, you can use DQL as follows:

$q = Doctrine_Core::getTable("myTable")->createQuery("q")
        ->update()
        ->where("id = ?", $id);

    foreach($requestContentObj as $key => $value)
    {
        $q->set($key, "?", $value);
    }

    $q->execute();
+2
source

Source: https://habr.com/ru/post/1536555/


All Articles