<title>[PHP] Yii2 The pit of the frame. md-/users/zjh/documents/my article/[php]yii2 the frame of the pit</title>
Several concealed pits of the Yii2 frame
Absrtact: Yii2 is an excellent general-purpose Web backend framework, which has the advantages of simple and elegant structure, rich practical functions, strong extensibility and performance. It is a good place in the process you can always easily find, do not need to repeat. And these small hidden flaws, it seems more necessary to tell you.
- Blog: Http://www.cnblogs.com/jhzhu
- E-mail: jhzhuustc@gmail.com
- Author: Know so
- Date: 2015-08-17
Directory
- Several concealed pits of the Yii2 frame
- Directory
- A little gossip.
- ActiveRecord is inexplicably written?
- Prepare knowledge
- Code site
- Summarize the problem
- Workaround
- Has your transaction come into effect?
- Code site
- Summary of issues
- Workaround
- ' y-m-d ' is not recognized?
- Code site
- Summary of issues
A little gossip.
It's been three months since I last wrote a blog. Before writing, always with a deep sense of guilt. Tortured by it for a long time, finally, still, hands.
One thing to celebrate: Recently, I got up 8:30 every day. There are two videos that are easy to use, just 8 minutes, and make sure you're cool (no) to (like) a blast (dead). (8-Minute abdominal workout level 2nd-Download, 8-minute chest muscle workout level 2nd-download)
One thing worth rethinking: the recent reading of Schopenhauer's aesthetic essay has changed the way I have always read. I used to think that reading is the source of progress, but the book has been deeply beaten face. Here, let's share a paragraph:
The external environment that we see is not like reading, imposing a certain set of ideas on our minds, but merely providing us with materials and opportunities. Think of things that are commensurate with our mental abilities and the emotions of the present. So, too much reading will be our mental loss of elasticity, just as the weight of a constant pressure on a spring will be the spring is not elastic, and let oneself do not have their own thoughts of the surest way is to pick up a book in every minute of free.
Thinking is the source of progress!
All right, rip it off and get to the chase.
ActiveRecord is inexplicably written?
Prepare knowledge
ActiveRecordBasic usage of the. If you do not understand, refer to here.
Code site
/** * @property integer $id * @property string $name * @property string $detail * @property Double $price * @property integer $area **/ class Ocroom extends Activierecord { ... }$room = Ocroom::find () //Take out an object first. ->select ([' ID ']) //Only take out ' ID ' column ->where ([' ID ' =>20]) ->one (); $room->save (); Save, you will find that the other fields on this line are written as default values.
Summarize the problem
The problem with this example is:
- I took a row out of the database, which is in the code
$room , but only the fields were taken out id , and the other fields were naturally the default values.
- When I do
$room->save() , the fields that are the default values are also saved in the database. What!?
- That is, when you want to conserve resources, do not take out all the fields, it is important to note that can not be saved, otherwise, a lot of data will be inexplicably modified to default values.
Workaround
However, what solutions do we have? provides several ideas:
- Keep an eye on yourself and avoid the preservation of incomplete removal
ActiveRecord .
- Modified or inherited
ActiveRecord so that when this object find() is created by a new, and the field is not completely fetched, the save() method is called, throwing an exception.
- Modify or inherit so
ActiveRecord that when this object is created by a find() new one, and the field is not fully removed, when the method is called, save() only the fields that have been removed are saved, and the other fields are ignored.
Has your transaction come into effect?
Code site
/*** @property integer $id* @property string $name **/class Ocroom extends ActiveRecord{Public function rules () {return [[' Name ', ' String ', ' min ' =>2, ' Max ' =>10]]; } ...}class Ochouse extends ActiveRecord{Public function rules () {return [[' Name ', ' String ', ' Max ' =>10]]; } ...}$a = new Ocroom ();$a->name = "; Name is an empty string and does not meet the rules () condition. $b = new Ochouse ();$b->name = ' my room '; Name is valid and can be saved. $transaction = Yii:: $app->db->begintransaction ();try{$a->save (); The name field is not valid, cannot be verified by, the Validate () stage has returned false, no database storage steps are made, so no exception is thrown. $b->save (); The name field is valid and can be saved normally. $transaction->commit (); After submission, a $ A save was found to fail while the $b was saved successfully. }catch (Exception $e){Yii::error ($e->gettraceasstring (), __method__);$transaction->rollback ();}
Summary of issues
The problem with this piece of code is:
- Everyone knows that
$transaction the existence of the meaning is to ensure that the entire database storage code is either full success, or all failed.
- Obviously, in this case,
transaction we didn't get the effect we wanted: because we never $a had it, we didn't get validate() $transation->commit() an error.
Workaround
$transationwithin the block, all of the values are judged by the save() return value, and if false so, the exception is thrown directly.
' y-m-d ' is not recognized?
Code site
Ocrenterbill extends ActiveRecord {public function rules () { return [ ' start_time ', ' Date ' , ' format ' = ' y-m-d ', ]; }}$a = new Ocrenterbill (); $a = ' 2015-09-12 '; $a->save (); Will error, say the wrong format.
Summary of issues
If the YII framework starts with an error, it's not a pit. The pit is when I was developing on a Mac, this could work perfectly normal, and after publishing to the online environment (Ubuntu), the "attribute start_time format is invalid" error pops up. In the official documentation, it is found that this format is an official document allowed.
Oh, ah ah. A variety of trial and error, and finally found that if changed php:Y-m-d , the world is pure. So, if you're having this problem, thank me.
The above describes the [Php]yii2 framework of the pit, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.