php - 如何在 YII2 模型上手动更改主键

标签 php yii2

我尝试使用这种方式,但结果是不完整的列名。

class VMaterial extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'v_material';
    }


    /**
     * @inheritdoc$primaryKey
     */
    public static function primaryKey()
    {
        return "id";
    }

结果:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'i' in 'where clause' The SQL being executed was: SELECT * FROM v_material WHERE i='8'

最佳答案

有两种方法可以解决您的问题:

class VMaterial extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'v_material';
    }

    /**
     * @inheritdoc$primaryKey
     */
    public static function primaryKey()
    {
        return ["id"];
    }
}

class VMaterial extends \yii\db\ActiveRecord
{
    public $primaryKey = "id";

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'v_material';
    }
}

关于php - 如何在 YII2 模型上手动更改主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47784840/

相关文章:

php - 获取所有没有特定前缀组的单词

Yii2 仅当日期时间字段大于今天时才查询记录

mysql - Yii 2 Gridview - 搜索查询生成不明确的字段

php - iOS 应用程序从 PHP 网络服务器获取结果的时间过长

php - 使用 COALESCE 返回带有导航数组页面信息的 META 标题值

error-handling - YII2 错误显示在 'layout' 中,它是在哪里引发的?

themes - yii2框架应用新主题

mysql - 在 windows7 的 yii2 中安装 mongodb 失败

php - 数据库; OR 语法无效

php - 显示数据库中的 1 张以上图像? (PHP、MySQL)