Some error solutions and suggestions in the use of PHP Yii framework

Source: Internet
Author: User
This article mainly introduces some error solutions and suggestions in the use of PHP Yii framework, covering the use of common functions such as enabling the transaction mechanism and associated tables. For more information, see

This article mainly introduces some error solutions and suggestions in the use of PHP Yii framework, covering the use of common functions such as enabling the transaction mechanism and associated tables. For more information, see

This article is intended to record the minor problem solutions in the Yii development process, which are incomplete, unauthoritative, and not a tutorial. I wrote it myself and thought it could solve the problem. I may use it later. Just remember.

1. Introduction of Js and Css files in Yii.
Let's start with the simplest problem. It's not a problem, but a syntax. Suppose that all our js files are placed in the js folder at the same layer as protected, and all css files are placed in the css folder at the same layer as protected. Well, the specification is like this... then we can write the corresponding view interface as follows. The parameters of css and js functions are different... (I called it for an hour ..)
The second parameter for registering a js file is the location where the js file is stored. Three options are available: CClientScript: POS_HEAD; Head; CClientScript: POS_BEGIN; and CClientScript :: POS_END is placed at the end of the Body. You do not need to enter it unless otherwise specified... the second parameter for registering a Css file is media. If you are interested, click here. It is still the default parameter...
For js like Jquery, using registerCoreScript will not cause inexplicable errors...

// Register the js file Yii: app ()-> clientScript-> registerScriptFile (Yii: app ()-> baseUrl. '/js/project1.js', CClientScript: POS_HEAD); // register the css file Yii: app ()-> clientScript-> registerCssFile (Yii: app () -> baseUrl. '/css/project1.css'); // register the Jquery file Yii: app ()-> clientScript-> registerCoreScript ('jquery ');

2. Yii isNewRecord repair
The isNewRecord attribute of the Model of Yii is very useful and can be discussed based on this attribute. However, if we have enabled the transaction mechanism or other circumstances, this record is not found in the database, but isNewRecord is flase, that is to say, it is no longer a new record. The solution is to use the primary key to access the database and determine whether it is a new record. before using this attribute, we need to process it as follows. The following Model is Post and the primary key is id:

If (! $ Model-> isNewRecord) {$ db_exist = Post: model ()-> findByPk ($ model-> id); if ($ db_exist = NULL) $ model-> isNewRecord = true ;}

3. Yii generate hidden input fields
Although it is easy to write an input field by yourself (not display: none), sometimes the format of the Yii form code is not enough. It is just one sentence...

<? Php echo $ form-> hiddenField ($ model, 'name');?> <? Php if ($ model-> isNewRecord) echo $ form-> hiddenField ($ model, 'path', array ('SIZE' => 60, 'maxlength' => 128, 'id' => 'path1');?>

4. Yii generate drop-down menu
In most cases, we need a drop-down menu in form. At this time, the Chtml listdata is very useful. If the fields in our database are only few possible, such as 0 and 1, you can write as follows:

Echo $ form-> dropDownList ($ model, 'is _ marry', array ('0' => 'no', '1' => 'yes '));

In this case, you can see the "yes" and "no" drop-down menus. When you select "yes", this field is set to 1 and "no" is set to 0. Of course, it is often not that simple. We can add a function in the Model to generate an array of the drop-down menu, and then call it in the view. The data of this function can be written by yourself or searched in the database. Listdata is used below, which specifically means that the id in the model is the key and the name is the value.

/* Write in the model */public function getUserOptions () {$ models = User: model ()-> findAll (); $ models = User: model () -> findAllByAttributes (array ('is _ regeister '=> '1'); return CHtml: listdata ($ models, 'id', 'name ');} /* write it in the view Interface */echo $ form-> dropDownList ($ model, 'user _ id', user: model ()-> getUserOptions ());

5. Yii enable transaction mechanism
When you save several records to the database at the same time, you may need to enable the transaction mechanism. Yii is easy to enable the transaction mechanism. Only three sentences are enough.

/* Enable the transaction mechanism */$ transaction = Yii: app ()-> db-> beginTransaction (); if try {/* succeeds, commit */$ transaction-> commit ();} catch (Exception $ e) {$ transaction-> rollBack ();}

Relatively complete:

If ($ _ POST ['modela']) {/* enable transaction mechanism */$ transaction = Yii: app ()-> db-> beginTransaction (); try {/* a bunch of logic is omitted here */$ modelA-> save (); $ modelB-> save (); /* If the operation succeeds, commit */$ transaction-> commit (); $ this-> redirect (array ('view ', 'id' => $ model-> id);} catch (Exception $ e) {$ transaction-> rollBack ();}}

However, I will generally be like the following. Please feel free to understand the advantages...

If ($ _ POST ['modela']) {/* enable transaction mechanism */$ transaction = Yii: app ()-> db-> beginTransaction (); try {$ validated = true;/* a bunch of logic is omitted here */$ valid = $ modelA-> save (); $ validated = $ valid & $ validated; /* a bunch of logic will be omitted here */$ valid = $ modelB-> save (); $ validated = $ valid & $ validated; /* if it succeeds, commit */if ($ validated) {$ transaction-> commit (); $ this-> redirect (array ('view ', 'id' => $ model-> id);} else {/* roll back if it is unsuccessful */$ transaction-> rollBack ();}} catch (Exception $ e) {$ transaction-> rollBack ();}}

6. An error occurred while querying the same field in the associated table.
Sometimes we create two tables, but the two tables have the same fields. If no additional settings are made when using CDbCriteria for the with association query, a query error occurs, the approximate meaning is that the Mysql statement is fuzzy. At this time, we can set an individual name in the master table, and then add the name when querying related fields.
For example, two models, Post, and User have an id, which can be written as follows:

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.