/** * Called after each successful save operation. * * @param bool $created True if this save created a new record * @param array $options Options passed from Model::save(). * @return void * @throws InternalErrorException * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#aftersave * @see Model::save() */ public function afterSave($created, $options = array()) { //LikesUser登録 if (isset($this->LikesUser->data['LikesUser'])) { if (!$this->LikesUser->data['LikesUser']['like_id']) { $this->LikesUser->data['LikesUser']['like_id'] = $this->data[$this->alias]['id']; } if (!$this->LikesUser->save(null, false)) { throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); } $likeCount = $this->LikesUser->find('count', array('recursive' => -1, 'conditions' => array('like_id' => $this->data[$this->alias]['id'], 'is_liked' => true))); $unlikeCount = $this->LikesUser->find('count', array('recursive' => -1, 'conditions' => array('like_id' => $this->data[$this->alias]['id'], 'is_liked' => false))); $update = array($this->alias . '.like_count' => $likeCount, $this->alias . '.unlike_count' => $unlikeCount); $conditions = array($this->alias . '.id' => $this->data[$this->alias]['id']); if (!$this->updateAll($update, $conditions)) { throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); } } parent::afterSave($created, $options); }
/** * Called during validation operations, before validation. Please note that custom * validation rules can be defined in $validate. * * @param array $options Options passed from Model::save(). * @return bool True if validate operation should continue, false to abort * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate * @see Model::save() */ public function beforeValidate($options = array()) { $this->validate = Hash::merge($this->validate, array('is_liked' => array('boolean' => array('rule' => array('boolean'), 'message' => __d('net_commons', 'Invalid request.'))))); return parent::beforeValidate($options); }