Entry.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\models;
  3. use app\models\Users;
  4. use \MongoDate;
  5. use lithium\util\Validator;
  6. Class entry extends \lithium\data\Model {
  7. public static function __init() {
  8. parent::__init();
  9. //Validators go here
  10. }
  11. public $validates = array(
  12. 'my_watched_episodes' => array('numeric', 'message' => 'please enter a number'),
  13. 'my_start_date' => array('date', 'message' => 'Please enter a valid date'),
  14. 'my_finish_date' => array('date', 'message' => 'Please enter a valid date'),
  15. 'my_score' => array(array('inRange' => array('min' => 0, 'max' => '10'), 'message' => 'Enter a valid score'),
  16. array('numeric', 'message' => 'Please enter a number')),
  17. //'my_status' => array('isValidStatus', 'message' => 'please enter valid status'),
  18. 'my_times_watched' => array('numeric', 'message' => 'This must be a number')
  19. );
  20. //Add timestamping to entries. :TODO:
  21. /*
  22. public function add($entity, $username)
  23. {
  24. var_dump($entity->_data);
  25. exit();
  26. $updateData = array('$push' => array('animelist' => $entity));
  27. $conditions = array('username' => $username);
  28. $result = Entry::update($updateData, $conditions, array('atomic' => false));
  29. return $result;
  30. }
  31. */
  32. //Got lazy, the proper way to do it is above, but needs a bit of fiddling.
  33. //The below code works, but forces mongo to resave the entire record, which tags
  34. //longer than just updating what has chaged.
  35. public function add($entity, $username)
  36. {
  37. $user = User::find('first', array('conditions' => compact('username')));
  38. $entity->created_on = new MongoDate();
  39. $entity->updated_on = new MongoDate();
  40. $user->animelist[] = $entity;
  41. if (Validator::check($entity->data(), $this->validates, array('skipEmpty' => 'true'))) {
  42. return $user->save(null, array('validate' => false));
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. }
  49. public function edit($entity, $username)
  50. {
  51. $user = User::find('first', array('conditions' => compact('username')));
  52. $entity->created_on = new MongoDate();
  53. $entity->updated_on = new MongoDate();
  54. $user->animelist[] = $entity;
  55. return $user->save(null, array('validate' => false));
  56. }
  57. }