Create a CRUD from a database view using Gii in Yii2

跟風遠走 提交于 2019-12-04 06:37:19

Simple add to your Model class

 public static function primaryKey()
{
    return ['id'];
}

I had the same issue once. You need to add the function getPrimaryKey to your model class.

public function getPrimaryKey($asArray=false){
    return "id";
}

You can find more details here: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#getPrimaryKey()-detail

This should allow you to use the CRUD generator and also take care of the "undefined index: i" error.

Anilop

The bug is about mysql, your table should have a PRIMARY KEY in table.

CREATE TABLE Persons (
    P_Id int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    PRIMARY KEY (P_Id)    
);

is OK because P_Id is PRIMARY KEY,

on the converse,

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

is not OK.

The key to solve this problem is let your table have at least one PRIMARY KEY attribute.

The simplest workarround that comes to my mind would be to, create table that has the same structure as the one your desire (with primary key field). Use that table to create your CRUD. Now just simply replace your newly created table with View.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!