User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. class User extends EMongoDocument
  3. {
  4. /**
  5. * A public variable should be defined for each key=>value you want in the
  6. * model. Just like if it were a column in a mysql database
  7. */
  8. public $username;
  9. public $email;
  10. public $personal_number;
  11. public $first_name;
  12. public $last_name;
  13. public $client;
  14. public $company;
  15. /**
  16. * this is similar to the get tableName() method. this returns tha name of the
  17. * document for this class. this should be in all lowercase.
  18. */
  19. public function getCollectionName()
  20. {
  21. return 'users';
  22. }
  23. /**
  24. * If we override this method to return something different than '_id',
  25. * internal methods as findByPk etc. will be using returned field name as a primary key
  26. * @return string|array field name of primary key, or array for composited key
  27. */
  28. public function primaryKey()
  29. {
  30. return 'personal_number';
  31. }
  32. /**
  33. * This is defined as normal. Nothing has changed here
  34. *
  35. * @return array
  36. */
  37. public function rules() {
  38. return array(
  39. array('personal_number, first_name, last_name', 'required'),
  40. );
  41. }
  42. /**
  43. * This returns attribute labels for each public variable that will be stored
  44. * as key in the database. Is defined just as normal with mysql
  45. *
  46. * @return array
  47. */
  48. public function attributeLabels()
  49. {
  50. return array(
  51. 'username' => 'UserName',
  52. 'email' => 'EMail',
  53. 'personal_number' => 'PN',
  54. 'first_name' => 'First Name',
  55. 'last_name' => 'Last Name',
  56. 'client' => 'Client',
  57. 'company' => 'Company',
  58. );
  59. }
  60. /**
  61. * Returns the class name just as nornal.
  62. *
  63. * @static
  64. * @param string $className
  65. * @return
  66. */
  67. public static function model($className = __CLASS__)
  68. {
  69. return parent::model($className);
  70. }
  71. /**
  72. * RELATIONS
  73. */
  74. /**
  75. * HAS ONE relation
  76. * @return
  77. */
  78. public function client()
  79. {
  80. return Client::model()->findByAttributes(array('client'=>$this->getPrimaryKey()));
  81. }
  82. /**
  83. * BELONGS TO relation
  84. * @return
  85. */
  86. public function company()
  87. {
  88. return Company::model()->findByPk($this->company);
  89. }
  90. /**
  91. * HAS MANY relation
  92. * assume we have an orders and client model where we want to see all orders
  93. * by the client this user HAS ONE of. So this will return all orders
  94. * belonging this the users client.
  95. * @return
  96. */
  97. public function orders()
  98. {
  99. return Orders::model()->findAllByAttributes(array('client_id'=>$this->getPrimaryKey()));
  100. }
  101. // This method would be in the posts model. the tags key in the document is
  102. // just an array of tag names.
  103. /**
  104. * MANY MANY relation ship
  105. * all you have to do is create another HAS MANY relationship in the other
  106. * model. For example. If you had posts and tags where a post can have many
  107. * tags and each tag can have many posts. You would define the two following
  108. * methods
  109. *
  110. * This setup is also assuming you have a separate Model for handling tags
  111. * that lists the mongo object _id's that belongs to it. And in each post you
  112. * have a list of tag names that this post has.
  113. * @return
  114. */
  115. public function tags()
  116. {
  117. return Posts::model()->findAllByAttributes(array('post_id'=>$this->post_id));
  118. }
  119. // This method would be in the tags model. each tag is a document that has a
  120. // relation to the objects _id.
  121. public function Posts()
  122. {
  123. return Tags::model()->findAllByAttributes(array('tag_id'=>$this->tag_id));
  124. }
  125. /**
  126. * Embedded Documents. All you need to do is list the models that will be used
  127. * as embedded documents in this fashion:
  128. * 'name' => 'Model'
  129. * Where 'name' is the name of this embedded document, and how it will appear
  130. * as a key in the document.And 'Model' is the name of the model that holds
  131. * the embedded documents information. For the example below, see the
  132. * UserAddress.php file for an example
  133. *
  134. * @return array
  135. */
  136. public function embeddedDocuments()
  137. {
  138. // property name => embedded document class name
  139. return array(
  140. 'address' => 'UserAddress',
  141. );
  142. }
  143. }