Wednesday, 15 April 2015

php - Field Data With ActiveForm -


On using the Yii framework, wanted to know what is the best way to calculate age from DOB.

I have a model with the following fields.

  'id' = & gt; 'Id', 'firstname' = & gt; 'Firstname', 'Surname' = & gt; 'Surname', 'dub' = & gt; 'Dob', 'age' = & gt; 'Age',  

Then in my _form.php file I am trying to edit it so that the age should be abandoned

   Field ($ Model, 'First Name') - & gt; Text input (['maximalit' => 50])? & Gt; & Lt ;? = $ Form-> Field ($ model, 'alias') - & gt; Text input (['max length' => 50])? & Gt; & Lt ;? = $ Form-> Field ($ model, 'dob') - & gt; Widget (DatePicker :: className (), [// inline also, not bad 'inline' = & gt; wrong, modify template // custom presentation 'template' = & gt; & lt; div class = "well -Sm "style =" background color: #fff; width: 250px "& gt; {input} & lt; / div & gt; ',' client option '= & gt; [' autoclose '= & gt; true , 'Format' = & gt; 'yyyy-mm-dd']]) ;? & Gt; & Lt ;? Php echo html :: activehide input ($ model, 'age'); & Lt; Div class = "form-group" & gt; & Lt ;? = Html :: submitButton ($ Model-> Hainai Record? Create ':' Update ', [' Class '=> $ Model-> Honeycomord?' BTN BTN-Success ':' BTN BTN- Primary '])? & Gt; & Lt; / Div & gt; & Lt ;? Php ActiveForm :: end (); ? & Gt; There is a problem to get the $ Model-> dub of course, the first form is to be submitted, so when the age calculation is drawn on.  

What do I want to know if I can read before the dub field so that the age can be correctly counted?

Or it is best done like a trigger in the database

Cheers.

P> In your user model, add:

 < Code> public $ age; Public function getAge () {// Age-MM-DD if age in format (ASAT ($ -> -> era)) this $ - $ other {$ dob = explosion ("-", $ this-> Dob); $ Age = (Date ("MD", Date ("U", MKTM (0, 0, 0, $ dob [1], $ dob [2], $ dob [0]))) Date (" MD ")? ((Date (" Y ") - $ dob [2]) - 1): (Date (" Y ") - $ dob [2])); $ This- & gt; Age = $ age; Return $ age; }}  

Then you can simply call $ model-> getAge () where you need it (if the $ model is the course of your user model .)


No comments:

Post a Comment