Guest User

Untitled

a guest
Mar 12th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * ApmModel is the base class of each models which use propel for data access
  5. * layer. This method will implement a __call method which will call the right
  6. * method of the model object.
  7. * @package core
  8. * @subpackage model
  9. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  10. * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  11. * @since 1.0.0
  12. * @version 1.0.0
  13. */
  14. class ApmModel extends AgaviModel
  15. {
  16. /**
  17. * @var object The propel object.
  18. */
  19. protected $propelobj = null;
  20.  
  21. /**
  22. * Set the propel object.
  23. * @param object The propel object.
  24. * @return void
  25. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  26. * @since 1.0.0
  27. */
  28. public function setPropelObject($obj)
  29. {
  30. $this->propelobj = $obj;
  31. }
  32.  
  33. /**
  34. * Return the propel object.
  35. * @return object The propel object.
  36. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  37. * @since 1.0.0
  38. */
  39. public function getPropelObject()
  40. {
  41. return $this->propelobj;
  42. }
  43.  
  44. /**
  45. * Convenience overload for accessing the user attributes with a simple
  46. * method call.
  47. * @param string The method name.
  48. * @param array The method arguments.
  49. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  50. * @since 1.0.0
  51. */
  52. public function __call($name, array $args)
  53. {
  54. if (preg_match('/^(get|set)(.+)$/', $name, $matches)) {
  55. if (method_exists($this->propelobj, $name) {
  56. return call_user_func_array(array($this->propelobj, $name), $args);
  57. }
  58. return null;
  59. }
  60. }
  61.  
  62. /**
  63. * If the object is new, it inserts it; otherwise an update is performed.
  64. * @return int The number of rows affected by this insert/update.
  65. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  66. * @since 1.0.0
  67. */
  68. public function save()
  69. {
  70. return $this->propelobj->save();
  71. }
  72.  
  73. /**
  74. * Removes this object from datastore and sets delete attribute.
  75. * @return void
  76. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  77. * @since 1.0.0
  78. */
  79. public function delete()
  80. {
  81. return $this->propelobj->delete();
  82. }
  83. }
  84.  
  85. // ---------------------------------------------------------------------
  86.  
  87. /**
  88. * ApmCustomerModel model is the business logic around a customer.
  89. * @package model
  90. * @subpackage models
  91. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  92. * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  93. * @since 1.0.0
  94. * @version 1.0.0
  95. */
  96. class ApmCustomerModel extends ApmModel
  97. {
  98. /*
  99. * Constructor. Initialize this model.
  100. * @return void
  101. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  102. * @since 1.0.0
  103. */
  104. public function __construct()
  105. {
  106. $this->setPropelObject(new DeskCustomerPeer());
  107. }
  108.  
  109. /*
  110. * Create a new customer based on the given attributes. A new instance of
  111. * the customer model will be return upon creation.
  112. * @param array The user attributes.
  113. * @return object The customer model.
  114. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  115. * @since 1.0.0
  116. */
  117. public function create(array $attrs)
  118. {
  119. $customer = $this->context->getModel('ApmCustomerModel');
  120. $customer->setCompany($attrs['company']);
  121. $customer->setFirstName($attrs['firstname']);
  122. $customer->setLastName($attrs['lastname']);
  123. $customer->setEmail($attrs['email']);
  124. $customer->setDateCreated(new DateTime());
  125. $customer->save();
  126. return $customer;
  127. }
  128.  
  129. /**
  130. * If the object is new, it inserts it; otherwise an update is performed and
  131. * the date of the update is saved.
  132. * @return int The number of rows affected by this insert/update.
  133. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  134. * @since 1.0.0
  135. */
  136. public function save()
  137. {
  138. $this->setDateUpdated(new DateTime());
  139. return parent::save();
  140. }
  141. }
  142.  
  143. // --------------------------------------------------------------------------
  144.  
  145. /**
  146. * ApmCustomerFinderModel model is the business logic around finding customers.
  147. * @package model
  148. * @subpackage models
  149. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  150. * @copyright Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  151. * @since 1.0.0
  152. * @version 1.0.0
  153. */
  154. class ApmCustomerFinderModel extends AgaviModel implements AgaviISingletonModel
  155. {
  156. /**
  157. * Find a given customer based on his id.
  158. * @param object The propel object.
  159. * @return void
  160. * @author Jean-Philippe Dery (jeanphilippe.dery@gmail.com)
  161. * @since 1.0.0
  162. */
  163. public function findById($id)
  164. {
  165. $customer = DeskCustomerPeer::retrieveByPk($id)
  166. if ($customer) {
  167. $model = $this->context->getModel('ApmCustomerModel');
  168. $model->setPropelObject($customer);
  169. return $model;
  170. }
  171. return null;
  172. }
  173. }
  174. ?>
Add Comment
Please, Sign In to add comment