Change the default controller in Yii2, add ' defaultroute ' = ' controller name ' in main.php,
Render rendered output view, renderpartial output view as-is.
Yii Validation Form form:
Backstage reception post data with $model->load (Yii:: $app->request->post ()), that is, to determine whether the foreground page has post data to pass over
$model->load (Yii:: $app->request->post () && $model->validate ()) determines whether the foreground page has post data, and the corresponding form form validation is passed.
Form validation components that require the introduction of form validation in the view, Use\widgets\activeform, starting with Activeform::begin () in the form, ending at Activeform::end ()
The controller introduces a set of form validation rules. When the view is output, the introduced validation rules are assigned to a variable output to the template, the template reference.
The validation rules that correspond to form forms must be written in a field that is consistent with the form form, otherwise the page cannot be submitted or validated.
In form validation, you can specify the content format and rules that users fill in:
return [
[[' username '], ' required ', ' message ' = ' user name cannot be empty '],
[' username ', ' string ', ' Max ' =>12, ' toolong ' = ' cannot exceed 12 '],
[' username ', ' string ', ' Max ' =>6, ' tooshort ' = ' cannot be less than 6 bits '],
[[' Email '], ' required ', ' message ' = ' password cannot be empty ',
[' username ', ' unique ', ' targetclass ' = ' frontend\models\pmd ', ' message ' = ' user name already exists '],
[' email ', ' unique ', ' targetclass ' = ' frontend\models\pmd ', ' message ' + ' email already exists, please change your email '],
[' email ', ' email ', ' message ' = ' email ' is not properly formatted '],
];
Yii additions and deletions change:
Yii, a model file with the same name as the table is created under model, and Yii automatically looks for the table. Select the model in the controller to choose the table. $table =modelname::find (). The corresponding operation succeeds with a return value of 1 for success of 0 for failure. We can see if the data operation was successful by judging the return value.
Query operation:
User::find ()->all (); This method returns all data;
User::findone ($id); This method returns a single piece of data for the primary key id=1;
User::find ()->where ([' name ' = ' username '])->one (); This method returns a piece of data [' name ' = ' username '];
User::find ()->where ([' name ' = ' username '])->all (); This method returns all data for [' name ' = ' username '];
User::findbysql (' SELECT * from user ')->all (); This method uses SQL statements to query all the data in the user table;
User::findbysql (' SELECT * from user ')->one (); This method uses an SQL statement to query a data in the user table;
User::find ()->andwhere ([' sex ' = ' male ', ' age ' = '] ')->count (' id '); Statistics of the total number of eligible articles;
User::find ()->one (); This method returns a single piece of data;
User::find ()->all (); This method returns all data;
User::find ()->count (); This method returns the number of records;
User::find ()->average (); This method returns the average of the specified column;
User::find ()->min (); This method returns the minimum value for the specified column;
User::find ()->max (); This method returns the maximum value of the specified column;
User::find ()->scalar (); This method returns the query result for the first column of the first row of the value;
User::find ()->column (); This method returns the value of the first column in the query result;
User::find ()->exists (); This method returns a value indicating whether the data row contains the query results;
User::find ()->batch (10); Fetch 10 data at a time
User::find ()->each (10); 10 data per fetch, iterative query
Add Action:
(http://blog.163.com/liu_wen_xin123/blog/static/112248632201241413519506/)
$model = new User ();
$model->username = ' username ';
$model->age = ' 20 ';
$model->insert ();//The current controller is the user controller and has a corresponding model table.
Delete operation:
User::d eleteall (' name = Username '); Delete the name = Username data;
User::findone ($id)->delete (); Delete the database where the primary key is the value of the $id variable;
User::d eleteall (' Age >: Age and sex =: Sex ', [': Age ' = ' + ', ': Sex ' = ' 1 ']); Delete the data that meets the criteria;
Update Modify Action:
You can change data using native sql: User::findbysql ().
Yii Learning Notes