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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?