Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the primarykey manually on the YII 2 model

Tags:

php

yii2

I tried to use this way, but the result is not complete column name.

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


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

RESULT :

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

like image 855
Rizal Widiantoro Avatar asked Dec 13 '17 02:12

Rizal Widiantoro


1 Answers

There are two ways to fix your problem:

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

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

or

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

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'v_material';
    }
}
like image 160
Paul Avatar answered Oct 17 '22 05:10

Paul