我尝试使用这种方式,但结果是不完整的列名。
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
WHEREi
='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/