Getting Started

Completely new to Yii, but attracted by its apparent straightforwardness and elegance, I thought the best bet for getting started would be Larry Ullman’s “Learning the Yii framework” step-by-step for building a simple two-table app (new posters are not allowed to post URLs).

The first five steps are a breeze. The generated app runs and looks OK. But with step six, progress grinds to a halt …

  1. The app has no menu pointing to CRUD forms for the two tables. Why not? How do I add that, to which file, and where’s the documentation for that step?

  2. I can manually bring up the Department form or the Employee form by adding /department or /employee to the main app’s url, and from there choose the Create option, but once again, functionality a framework could generate automatically is not there, for example in the employee form …

2a. There’s a field for choosing the department to which the employee belongs. The input control is a plain text input. The model stipulates this is a foreign key. Why doesn’t Yii generate a lookup dropdown? Yii doesn’t do it, I have to do it, but how do I add it, to which file, and where’s the documentation for that step?

2b. There’s an email field; why doesn’t Yii generate an automatic email address validator? I have to do it. How do I add it, to which file, and where’s the documentation for that?

2c. There’s a HireDate field that needs a data picker, but Yii does not provide it. Why not? I have to do it, but how do I add it, to which file, and where’s the documentation for that?

  1. Is there a set of model configurations I can set so Yii generates such code automatically in future apps? If so, how do I do that, which file, and where is the documented for that?

Sorry to have so many questions. I’d really like to like this framework. Any help appreciated.

Ok, first of all keep calm and take it easy!

Read all the definitive guide to Yii, in which there are the answers nearly to all questions about "wich file and where is the documentation for that".

The generator is not supposed to do all the job for you, but to create the structure of the code in order to let you easy to edit.

More specific, all code you find in protected is supposed to be edited (yes, you have to do it), so read the documentation of all class and method you find in it, expecially controller and model.

Read the documentation about model and controller, and take a look at this wiki that explains a bit where is the doc of what.

About this point, you can create new code generators.

Take a look at this paragraph of the definitive guide

Thanks for replying.

The more code the framework auto-generates, the more useful the framework is to me.

One has to read the entire guide before developing a 2-table demo app? That’s discouraging.

I’ve read the model and controller material you point to. Can’t discern answers to my questions in those pages, unfortunately.

I’ve read much of the material suggested by zaccaria. Unfortunately I couldn’t find answers to my questions.

Based on what I read and found on the web, I tried several mods to the Ullman sample app. They all failed with exception reports.

If I could just get one form mod working, I might find a way forward.

So will someone tell me please, given the Employee form as generated up to step 5 in the Ullman test app, how to edit /protected/views/_form.php (or some other file if _form.php is the wrong file) to either …

  • add a lookup dropdown to the Department ID input control, OR …

_ add a date picker to the hiredate input control that initialises to, say, the current date

Many thanks in advance.

So you want to edit /protected/views/employee/_form.php I guess.

[list=1]

[*]For the first modification, search for the code that displays your departmentId, and change the line


<?php echo $form->textField($model, 'departmentId', …); ?>

into


<?php echo $form->dropDownList($model, 'departmentId',

               CHtml::listData(Department::model()->findAll(), 'id', 'name'),

               array('empty' => 'Select…')

           ); ?>

Here I used CActiveForm::dropDownList method (helper) that displays data array built by CHtml::listData taking as input the data retrieved by CActiveRecord::findAll method

[*]For the second modification, search for the code that displays your hireDate, and change the line


<?php echo $form->textField($model, 'hireDate', …); ?>

into


<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

          'model' => $model,

          'attribute' => 'hireDate',

          'language' => Yii::app()->language,

      )); ?>

Here I used CJuiDatepicker

[/list]

You can also check giix extension, it’s a great CRUD tool (more complete, but you’ll still have view code to adapt). I believe there are other ones for CRUD, but I haven’t used them.

Now, that said, you can easily find what I replied you above on this very forum, asked and answered several times, and I believe on Larry Ullman’s tutorials as well.

As a matter of fact, for #1

Thank you for your reply. Both your examples work.

?! Before posting any of these questions, I searched and followed several answers. Many opened into long threads. I tried several suggested code blocks. None worked.

His page seven gives several examples. I was able to abstract from none of them to my questions.