i have other apis working added table has user_id
id field instead of id
, api says object not found. there way override field api looks userinfos/1
?
where log users/12
select * `user` `id`='12'
i error userinfos/12
because id user_id in user_info table:
exception 'yii\web\notfoundhttpexception' message 'object not found: 12' in /opt/decathletics/vendor/yiisoft/yii2/rest/action.php:101
it should select * user_info user_id='12'
instead above error
userinfocontroller.php
<?php namespace api\modules\v1\controllers; use yii; use yii\rest\activecontroller; use yii\filters\auth\httpbasicauth; use yii\filters\verbfilter; use yii\filters\accesscontrol; use common\models\user; use api\modules\v1\models\userinfo; class userinfocontroller extends activecontroller { public function behaviors() { $behaviors = parent::behaviors(); $behaviors['authenticator'] = [ 'class' => httpbasicauth::classname(), ]; $behaviors['access'] = [ 'class' => accesscontrol::classname(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchcallback' => function ($rule, $action) { return yii::$app->user->id == yii::$app->request->get('id'); }, 'verbs' => ['get','put'] ], ], ]; return $behaviors; } public $modelclass = 'api\modules\v1\models\userinfo'; }
userinfo.php model
<?php namespace api\modules\v1\models; use yii; class userinfo extends \yii\db\activerecord { /** * @inheritdoc */ public static function tablename() { return 'user_info'; } /** * @inheritdoc */ public function rules() { return [ [['user_id'], 'required'], [['user_id'], 'integer'], ... ]; } /** * @inheritdoc */ public function attributelabels() { return [ 'user_id' => 'user id', ... ]; } }
activecontroller use yii\rest\action::findmodel($id)
data based on model primary key declared in db table. in case may try check database schema see if user_id
defined primary key of user_info
table (and not id
instead) or can try manually override model's primarykey()
method adding :
class userinfo extends \yii\db\activerecord { ... public static function primarykey() { return ['user_id']; } ...
Comments
Post a Comment