Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $request = Request::createFromGlobals();
- $requestContentJSON = $request->getContent();
- $requestContentObj = json_decode($requestContentJSON);
- $repository = $this->getDoctrine()->getRepository('MyBundle:Company');
- $company = $repository->find($id);
- 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;
- }
- }
- $company->set("property", "value");
- 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 = ('email' => 'abc.@gmail.com');
- $customer = new Customer($array);
- echo $customer->getEmail();
- <?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"
- }
- $q = Doctrine_Core::getTable("myTable")->createQuery("q")
- ->update()
- ->where("id = ?", $id);
- foreach($requestContentObj as $key => $value)
- {
- $q->set($key, "?", $value);
- }
- $q->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement