dqaxw44567 2016-01-24 18:41
浏览 88
已采纳

当id字段未命名为id时,找不到Yii2 api对象

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',
            ...
        ];
    }


}
  • 写回答

1条回答 默认 最新

  • doufan1363 2016-01-24 19:57
    关注

    ActiveController will use yiiest\Action::findModel($id) to get your data based on its model primary key as declared in the DB table. In your case you may try to check your database schema to see if user_id is really defined as Primary Key of the user_info table (and not id instead) or you can try to manually override your model's primaryKey() method by adding this :

    class UserInfo extends \yii\db\ActiveRecord
    {
      ...
    
      public static function primaryKey()
      {
         return ['user_id'];
      }
    
      ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错