Entry.php 1.9 KB

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