Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. $request = Request::createFromGlobals();
  2. $requestContentJSON = $request->getContent();
  3. $requestContentObj = json_decode($requestContentJSON);
  4.  
  5. $repository = $this->getDoctrine()->getRepository('MyBundle:Company');
  6. $company = $repository->find($id);
  7.  
  8. foreach($requestContentObj as $key => $value){
  9. switch($key){
  10. case 'name':
  11. $company->setName($value);
  12. break;
  13. case 'department':
  14. $company->setDepartment($value);
  15. break;
  16. case 'origin':
  17. $company->setOrigin($value);
  18. break;
  19. case 'headquarters':
  20. $company->setHeadquarters($value);
  21. break;
  22. case 'email':
  23. $company->setEmail($value);
  24. break;
  25. case 'twitterid':
  26. $company->setTwitterId($value);
  27. break;
  28. case 'description':
  29. $company->setDescription($value);
  30. break;
  31. }
  32. }
  33.  
  34. $company->set("property", "value");
  35.  
  36. Class customer {
  37.  
  38. protected $_email;
  39.  
  40. public function __construct(array $config = array()){
  41. $this->setOptions($config);
  42. }
  43.  
  44. public function getEmail(){
  45. return $this->_email;
  46. }
  47.  
  48. public function setEmail($email){
  49. $this->_email = $email;
  50. }
  51.  
  52. public function setOptions(array $options)
  53. {
  54. $_classMethods = get_class_methods($this);
  55. foreach ($options as $key => $value) {
  56. $method = 'set' . ucfirst($key);
  57. if (in_array($method, $_classMethods)) {
  58. $this->$method($value);
  59. } else {
  60. throw new Exception('Invalid method name');
  61. }
  62. }
  63. return $this;
  64. }
  65.  
  66. public function setOption($key, $value){
  67. return $this->setOptions(array($key, $value));
  68. }
  69.  
  70. }
  71.  
  72. $array = ('email' => 'abc.@gmail.com');
  73. $customer = new Customer($array);
  74. echo $customer->getEmail();
  75.  
  76. <?php
  77.  
  78. // example Company entity
  79. class Company
  80. {
  81. private $name;
  82.  
  83. function setName($name)
  84. {
  85. $this->name = $name;
  86. }
  87.  
  88. function getName()
  89. {
  90. return $this->name;
  91. }
  92.  
  93. function merge(stdClass $obj)
  94. {
  95. // get the object vars of the passed object
  96. // iterate, and replace matching properties
  97. foreach (get_object_vars($obj) as $prop => $val) {
  98. if (property_exists($this, $prop)) {
  99. $this->$prop = $val;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. $company = new Company();
  106.  
  107. // mocking your request object
  108. $requestContentObj = new stdClass();
  109. $requestContentObj->name = 'acme';
  110.  
  111. $company->merge($requestContentObj);
  112.  
  113. var_dump($company);
  114.  
  115. class Company#1 (1) {
  116. private $name =>
  117. string(4) "acme"
  118. }
  119.  
  120. $q = Doctrine_Core::getTable("myTable")->createQuery("q")
  121. ->update()
  122. ->where("id = ?", $id);
  123.  
  124. foreach($requestContentObj as $key => $value)
  125. {
  126. $q->set($key, "?", $value);
  127. }
  128.  
  129. $q->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement