Esempio n. 1
0
 /**
  * REST-ful API method for adding and removing relationships between records.
  */
 public function actionRelationship()
 {
     $rType = Yii::app()->request->requestType;
     switch ($rType) {
         case 'GET':
             // Look up relationships on a model
             $attr = array('firstType' => $_GET['model']);
             $relationships = Relationships::model()->findAllByAttributes(array_merge(array_intersect_key($_GET, array_flip(Relationships::model()->safeAttributeNames)), $attr));
             if (empty($relationships)) {
                 $this->_sendResponse(404, Yii::t('api', 'No relationships found.'));
             } else {
                 $this->_sendResponse(200, array_map(function ($r) {
                     return $r->attributes;
                 }, $relationships), 1);
             }
         case 'POST':
             // Add a new relationship to model
             $relationship = new Relationships('api');
             $relationship->attributes = $_POST;
             $relationship->firstType = $_GET['model'];
             if ($relationship->validate()) {
                 $existingRelationship = Relationships::model()->findByAttributes(array_intersect_key($relationship->attributes, array_flip(array('firstType', 'secondType', 'firstId', 'secondId'))));
                 if ($existingRelationship) {
                     $this->_sendResponse(200, Yii::t('api', 'Such a relationship already exists.'));
                 }
                 if ($relationship->save()) {
                     $this->_sendResponse(200, Yii::t('api', 'Successfully saved a relationship.'));
                 } else {
                     $this->_sendResponse(500, Yii::t('api', 'Failed to save relationship record for unknown reason.'));
                 }
             } else {
                 $this->response['modelErrors'] = $relationship->errors;
                 $this->_sendResponse(400, $this->validationMsg('create', $relationship));
             }
             break;
         case 'DELETE':
             if (!isset($_GET['secondType'], $_GET['firstId'], $_GET['secondId'])) {
                 $this->_sendResponse(400, Yii::t('api', 'Cannot delete; no parameters specified for finding a relationship record to delete.'));
             }
             $relationships = Relationships::model()->findAllByAttributes(array_merge(array('firstType' => $_GET['model']), array_intersect_key($_GET, array_flip(Relationships::model()->attributeNames()))));
             if (empty($relationships)) {
                 $this->_sendResponse(404, Yii::t('api', 'No relationships deleted; none were found matching specfied parameters.'));
             }
             $n_d = 0;
             $n_d_t = count($relationships);
             foreach ($relationships as $model) {
                 $n_d += $model->delete() ? 1 : 0;
             }
             if ($n_d == $n_d_t) {
                 $this->_sendResponse(200, Yii::t('api', '{n} relationships deleted.', array('{n}' => $n_d)));
             } else {
                 $this->_sendResponse(500, Yii::t('api', 'One or more relationships could not be deleted.'));
             }
             break;
         default:
             $this->_sendResponse(400, Yii::t('api', 'Request type not supported for this action.'));
             break;
     }
 }