Hello and my greetings and congrats to Yii team for such a nice framework.
I am new to Yii and to this forum.
I have just installed advanced application template for yii2.
Now I am faced with couple of issues at the very start.
1.When creating CRUD for Model in Gii it is now asking too many questions. Need to understand or a guideline for choosing namespace correctly or any undesirable affect in not following the norms.
When trying to use the date picker widget in the active form I have inserted this code
So the question is am I right in adding path information to DatePicker or I have not configured the Yii correctly?
Again I wanted to change the Date format to ‘dd-mm-yy’ and edited the code accordingly. The input start showing the correct format, but on save, the result was ‘0000-00-00’. If I keep the format as it is, the dates are getting saved correctly.
But How about the date formatting? I am not able to save the dates with date formatted as ‘dd-mm-yy’ as also how to create namespaces while generating models and CRUD?
For the second part of your query: you may want to read about namespaces in PHP. If you check any class in Yii you have at the top – the namespace for the class… for example your model class:
namespace frontend\models;
use yii\base\Model;
class YourModel extends Model {
// your code
}
For the first part of your query: depends on how your database field is set (e.g. is it INT or TIMESTAMP or DATETIME for MySQL). You could handle that transformation in your model beforeSave() or the controller action before calling $model->save() - something like this in your model:
public function beforeSave($insert) {
// unix timestamp
$time = strtotime($model->admission_date);
// if you want a specific format
$time = date("Y-m-d", strtotime($model->admission_date));
// any other custom validations you need for your date time
// e.g. isTheTimeOk($time);
if (isTheTimeOk($time)) {
$model->admission_date = $time;
return parent::beforeSave($insert);
}
else {
return false;
}
}
BUT… a better method… in your case, since you require to just format the date (if you are already saving the date as unix timestamp)… simply you can use this: