I have other apis working but I just added a table that has user_id
as the id field instead of id
and now the api says the object is not found. Is there a way to override the field the api looks for with userinfos/1
?
Where the log will usually say this for users/12
SELECT * FROM `user` WHERE `id`='12'
I get this error for userinfos/12
because id is actually user_id in the user_info table:
exception 'yii\web\NotFoundHttpException' with message 'Object not found: 12' in /opt/decathletics/vendor/yiisoft/yii2/rest/Action.php:101
It should say SELECT * FROM user_info WHERE user_id='12'
but instead I get the above error
UserinfoController.php
<?php
namespace api\modules\v1\controllers;
use Yii;
use yiiest\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',
...
];
}
}