Create a CRUD from a database view using Gii in Yii2

Create a CRUD from a database view using Gii in Yii2

how to create module in yii2 using gii
yii2 tutorial
yii application
yii2 website
yii2 latest version
yyi 2

I have generated a Model using gii of a mariadb view, which worked.

Then trying use the gii CRUD generator for the model, I get the error

The table associated with app\models\Future must have primary key(s).

Which is perfectly understandable as the the view does not have a PK. I found some advice that says to add a primaryKey function to the model so I tried

public function primaryKey()
    return 'id';

With id being the column name which is actually the PK in the underlying table, which is part of the view. But this fails with an exception

Cannot make static method yii\db\ActiveRecord::primaryKey() non static in class app\models\Future

So I tried making the method static but it then throws new exception

Undefined index: i
1. in /home/adrian/projects/mtview/mtview/vendor/yiisoft/yii2-gii/generators/crud/Generator.php at line 509

Is there a way around this, or is it now impossible to use gii to generate code for Database views?

Simple add to your Model class

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

Getting Started: Generating Code with Gii, Using Gii to auto-generate code is simply a matter of entering the right information use Gii to generate the code implementing the CRUD operations for a DB table, For each country displayed in the grid, you may choose to view its details,  Super Easy CRUD With Gii And The Yii2 Framework. Gii is an incredible piece of software that is part of the Yii Framework. It is a wonderful tool that writes code for you. So many of the development requirements of web applications in PHP include repetitive code, which is why we have these frameworks in the first place.

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:

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

Getting Started: Basic Usage | Gii Extension for Yii 2, CRUD Generator - This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model. When the file already exists, Gii also provides a diff view that shows what is different Tip: When using the Model Generator to update models after database  Using Gii to auto-generate code is simply a matter of entering the right information per the instructions shown on the Gii Web pages. Through this tutorial, you will learn how to: enable Gii in your application, use Gii to generate an Active Record class, use Gii to generate the code implementing the CRUD operations for a DB table,

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

    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,

    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.

Super Easy CRUD With Gii And The Yii2 Framework – Vegibit, Not only did Gii create our view for us, it gave us the code that we'll need for our All we did was create a database table, use Gii to generate a model, then  – To create a model, we must using \yii\db\ActiveRecord – Look function tableName(), put your table name to that function. – Function rules() used for filtering your input. If you using ‘required’ like above, then the input (full_name, address, phone) must be filled. ‘max’ => 100 is script to limit the characters that you input.

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.

PHP Master, In this article I'll cover the basics using Yii to create a CRUD system. Since we'll be working with a MySQL database, you should have the MYSQL We can create a view file for our controller called create.php it in the above To use gii in your project, find the following in your application's config file and  To configure your database in Yii2, go to common/config/main-local.php file and write the name of your database. We have written student as the name of our database. Next Topic YII Create (Insert) Record ← prev next →

How to Create a To-do List Using Gii in Yii2, Learn how you can use Gii module in Yii2 framework to create a simple to-do list. generate fully customized forms, models, CRUD for database and more For this, go to the todo folder inside the views and open _form.php. Creating a Simple CRUD App with Yii Framework 2 (Updated December 20th, 2013) ¶ By popular demand, today I will be revisiting my previous Yii Framework 2 Getting Started Guide and updating it with what is currently available in the Yii Framework 2 Repository. Provide here is an super simple example CRUD application that will help you get started using the framework.

Yii - Gii, Yii - Gii - Gii is the extension, that provides a web-based code generator for generating models, forms, modules, CRUD, and so forth. Form Generator − Generates a view script file that displays a form to collect input for the specified model class. Create a new database via CREATE DATABASE helloworld CHARACTER  In order to create a CRUD for this table, we need to create its model first. Now, we don’t need to write the code for it; I will be using Gii Model Generator for it. Let’s do it. Generating Module Using Gii. Now open Gii in the browser and click Start under Model generator.

Yii 2 - 4 Generating CRUD with GII, In part two, we learned about Yii's implementation of its Model View Controller Now that we've created the database table, we can begin to use Gii, Yii's Return to the Gii home page and click CRUD Generator, e.g.  Simple Yii Script for performing CRUD operations: Greetings, This tutorial teach you the simple way to perform the crud operation in Yii framework, its easy to perform the database operation in Yii. Here we are going to see the create, update, delete and select scripts that required in almost all the applications.