|
- <?php
-
-
- namespace yii\rest;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\db\ActiveRecordInterface;
- use yii\web\NotFoundHttpException;
-
-
- class Action extends \yii\base\Action
- {
-
-
- public $modelClass;
-
-
- public $findModel;
-
-
- public $checkAccess;
-
-
-
-
- public function init()
- {
- if ($this->modelClass === null) {
- throw new InvalidConfigException(get_class($this) . '::$modelClass must be set.');
- }
- }
-
-
-
- public function findModel($id)
- {
- if ($this->findModel !== null) {
- return call_user_func($this->findModel, $id, $this);
- }
-
-
- $modelClass = $this->modelClass;
- $keys = $modelClass::primaryKey();
- if (count($keys) > 1) {
- $values = explode(',', $id);
- if (count($keys) === count($values)) {
- $model = $modelClass::findOne(array_combine($keys, $values));
- }
- } elseif ($id !== null) {
- $model = $modelClass::findOne($id);
- }
-
- if (isset($model)) {
- return $model;
- } else {
- throw new NotFoundHttpException("Object not found: $id");
- }
- }
- }
|