Entry.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. }
  10. public $_schema = array('_id' => array('type' => 'id'));
  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. public function add($entity, $username)
  22. {
  23. $updateData = array('$push' => array('animelist' => $entity->data()));
  24. $conditions = compact('username');
  25. $result = Entry::update($updateData, $conditions, array('atomic' => false));
  26. return $result;
  27. }
  28. /*
  29. //Got lazy, the proper way to do it is above, but needs a bit of fiddling.
  30. //The below code works, but forces mongo to resave the entire record, which takes
  31. //longer than just updating what has chaged.
  32. public function add($entity, $username)
  33. {
  34. $user = User::find('first', array('conditions' => compact('username')));
  35. $entity->created_on = new MongoDate();
  36. $entity->updated_on = new MongoDate();
  37. $user->animelist[] = $entity;
  38. //if (Validator::check($entity->data(), $this->validates, array('skipEmpty' => 'true'))) {
  39. $return = $user->save(null, array('validate' => false));
  40. var_dump($entity);
  41. exit();
  42. /*
  43. else
  44. {
  45. return false;
  46. }
  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. }