| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace app\models;
- use app\models\Users;
- use \MongoDate;
- use lithium\util\Validator;
- Class entry extends \lithium\data\Model {
- public static function __init() {
- parent::__init();
- //Validators go here
- }
- public $validates = array(
- 'my_watched_episodes' => array('numeric', 'message' => 'please enter a number'),
- 'my_start_date' => array('date', 'message' => 'Please enter a valid date'),
- 'my_finish_date' => array('date', 'message' => 'Please enter a valid date'),
- 'my_score' => array(array('inRange' => array('min' => 0, 'max' => '10'), 'message' => 'Enter a valid score'),
- array('numeric', 'message' => 'Please enter a number')),
- //'my_status' => array('isValidStatus', 'message' => 'please enter valid status'),
- 'my_times_watched' => array('numeric', 'message' => 'This must be a number')
- );
- //Add timestamping to entries. :TODO:
-
- /*
- public function add($entity, $username)
- {
- var_dump($entity->_data);
- exit();
- $updateData = array('$push' => array('animelist' => $entity));
- $conditions = array('username' => $username);
- $result = Entry::update($updateData, $conditions, array('atomic' => false));
- return $result;
- }
- */
- //Got lazy, the proper way to do it is above, but needs a bit of fiddling.
- //The below code works, but forces mongo to resave the entire record, which tags
- //longer than just updating what has chaged.
- public function add($entity, $username)
- {
- $user = User::find('first', array('conditions' => compact('username')));
- $entity->created_on = new MongoDate();
- $entity->updated_on = new MongoDate();
- $user->animelist[] = $entity;
- if (Validator::check($entity->data(), $this->validates, array('skipEmpty' => 'true'))) {
- return $user->save(null, array('validate' => false));
- }
- else
- {
- return false;
- }
- }
- public function edit($entity, $username)
- {
- $user = User::find('first', array('conditions' => compact('username')));
- $entity->created_on = new MongoDate();
- $entity->updated_on = new MongoDate();
- $user->animelist[] = $entity;
- return $user->save(null, array('validate' => false));
- }
- }
|