Basic knowledge about the Yii framework and basic knowledge of the Yii framework

Source: Internet
Author: User

Basic knowledge about the Yii framework and basic knowledge of the Yii framework

The first time I wrote a blog post, I did not know how to write it. I was not very familiar with it. I learned the Yii framework I used everyday.

There are many development frameworks in PHP, such as ThinkPHP, Yii, CI, Laravel, and Phalcon. The Laravel framework is currently the most popular. Given that the company uses the Yii framework, let's talk about the basic operations of the Yii framework. If you want to write well, you can use it for reference. If you do not write well, you can give me some suggestions.

1.Yii framework is a framework with MVC ideas, Model, View, Controller. This framework was developed by Chinese Americans and is a heavyweight framework compared to other frameworks. The Yii framework has the scaffolding function, which can automatically generate the directory structure (but I usually don't need it ).

 

 

2.Scaffolding Function

Use Yii scaffold function to generate directory structure

(1) Open the Command Prompt window (Win7 is opened as an administrator)

(2) switch to the framework folder in the project.

(3) enter the following command to generate a directory

Yiic webapp ../app

Error Message: "Php.exe" is not an internal or external command,

Solution:Configure the php installation directory to the path environment variable of the system.

Path environment variable location: My computer-> properties-> advanced-> environment variables-> system variables-> Path

Variable name: Path

Variable value: adsfasdfasdfas; D: \ php5.4

Note: You must close the Command Prompt window and open it again.

 

 

3.Yii project directory

App -------------------- application directory

Assets ------------- third-party resource package *******

Css ---------------- style sheet *******

Images ------------- image *******

Protected ------------ Application

Commands ----------- system command

Components --------- component directory (custom class )*******

Controller. php --- parent class of all controllers

Config ------------- configure the folder *******

Main. php --------- global configuration file

Controllers -------- controller *******

Data --------------- database script *******

Extensions --------- extension class

Messages ----------- translated information

Migrations --------- database migration File

Models ------------- model *******

Runtime ------------ runtime

Views -------------- view *******

Layouts ---------- layout folder

Themes ------------- topic directory

Index. php ------------ entry file

Framework -------------- yii Resource Package

 

 

4.Naming rules

Controller naming rules

(1) The naming rules of the big hump must be adopted.

(2) It must end with a Controller.

IndexController. php

LoginController. php

Name of the method in the Controller

(1) The naming rules for small hump must be adopted.

(2) It must start with action.

ActionIndex ()

ActionAdd ()

ActionInsert ()

ActionLogin ()

Model naming rules

(1) The naming rules of the big hump must be adopted.

(2) The table name must be used.

BbsInfo. php

Reviews. php

 

 

5.Modify configuration file

(1) set the default Controller

"DefaultController" => "Controller/method ",

"DefaultController" => "index/index ",

(2) set the url route and enable pathinfo

/*

"UrlManager" => array (

......

),

*/

Remove the comments of the above Code

(3) set database configuration information

/*

"Db" => array (

......

),

*/

Remove the comments of the above Code and modify the database information.

 

 

6.The Controller displays the specified view.

(1) display the specified view without calling the layout File

$ This-> renderPartial ("view name ");

$ This-> renderPartial ("view name", array (name => value, name => value ...));

Note:An array transmits values to the view.

 

(2) display the specified view, but call the layout File

 

$ This-> render ("view name ");

 

$ This-> render ("view name", array (name => value, name => value ...));

(3) You can use include to include pages.

Include ($ this-> viewpath. "/index. php ");

 

 

7.Database Operations

(1) query multiple records and return two-dimensional array

$ Result = $ model-> findAll ();

$ Result = $ model-> findAll ("condition ");

$ Result = $ model-> findAll (array (

"Condition" => "condition ",

"Order" => "sort ",

"Offset" => start value,

"Limit" => Number of items

));

$ Result = $ model-> findAllBySql ("select statement ");

 

(2 ),Delete record,Returned Number of affected rows

 

$ Result = $ model-> deleteAll ();

 

$ Result = $ model-> deleteAll ("condition ");

 

$ Result = $ model-> deleteByPk (primary key );

 

(3 ),Query a record, Return: one-dimensional joined Array

 

$ Result = $ model-> find ();

 

$ Result = $ model-> find (condition );

 

$ Result = $ model-> findByPk (primary key );

 

$ Result = $ model-> findBySql ("select statement ");

 

(4 ),Modify record, Return: Number of affected rows

 

$ Result = $ model-> updateAll (one-dimensional joined array); // modify all records in the table

 

$ Result = $ model-> updateAll (one-dimensional join array, condition); // modify the record that meets the condition

 

$ Result = $ model-> updateByPk (primary key, one-dimensional associated array );

 

(5 ),Add record, Return: Number of affected rows

 

$ Model-> Field name = field value;

 

$ Model-> Field name = field value;

 

$ Model-> Field name = field value;

 

$ Result = $ model-> save ();

 

(6) execute the select statement and return a two-dimensional array.

 

$ Db = Yii: app ()-> db;

 

$ St = $ db-> createCommand ("select statement ");

 

$ Rs = $ st-> queryAll (); // obtain all records

 

(7) execute the select statement and return a one-dimensional array.

 

$ Db = Yii: app ()-> db;

 

$ St = $ db-> createCommand ("select statement ");

 

$ Rs = $ st-> queryRow (); // obtain a record

 

(8) execute the insert, update, and delete statements and return the number of affected rows.

 

$ Db = Yii: app ()-> db;

 

$ St = $ db-> createCommand ("insert | update | delete statement ");

 

$ Result = $ st-> execute ();

(9 ),Count QueryReturn: Number

$ Result = $ model-> count ();

$ Result = $ model-> count (condition); // query records that meet the condition

$ Result = $ model-> countBySql ("select statement ");

Note: The Yii framework only encapsulates count () for Aggregate functions. To use other Aggregate functions, use eg:

$ Result = $ model-> countBySql ("select max (clickTimes) from bbsInfo ");

 

 

In fact, many of them are flexible and not so rigid. I have shared this for the time being. I will share it later.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.