Yii Basic Steps
Posted: Apr 06, 2012 16:04;
Last Modified: May 23, 2012 18:05

This is just a reminder to myself about setting up a Yii install. There are much more detailed examples on the web.
Build database
Build a database (I use MySql with innodb engine). The innodb engine is important because it allows you to document foreign key associations
Copy yii to top level of webdirectory
If you haven’t already done so, get a copy of the latest version of yii, uncompress it, and install it in a webdirectory immediately below the web root.
In my case, I tend to put it under /var/www/ and call the directory yii.
Use yiic
to build initial site
navigate to the root of your web directory. Assuming yii is an immediate child of this directory, use the following to build the initial site of your website, where $site
is the directory name you want to use for your site and $path
the full path to the current directory:
The response should be: Your application has been created successfully under $path/$site
Prepare for building the scaffolding
change directory to $path/$site/protected/config/
edit main.php
:
- remove the comments surrounding the section on the gii tool (you’ll want to put these back in production)
- make sure you put a password in
- (for MySql) remove the comments surrounding the MySql Stanza and modify/supply the required information (usernames, passwords, servers, etc.)
- fix the admin email.
Use gii to establish the basic scaffolding
- point your webbrowser at the site (e.g.
http://localhost/$site/
orhttp://example.com/$site/
) - click on the “home” link in the menubar to get the top URL (it will be something like this:
http://$domain/$site/index.php?r=site/index
) - in the location bar, replace
site/index
withgii
- enter the database root password
Develop models
Most if not all dbases will have a model in the system.
- Select “Model Generator”.
- enter
*
in the “Table name” field to generate models for all tables; otherwise type the table name in the box for a specific table. - click on preview.
- if everything is good, click on generate (need to make sure permissions all generation).
Develop CRUD
- select “crud generator”
- enter Model names for the models for which you want to have specific forms and actions (not all tables will require this: some will be written automatically or through relations)

Adding an attribute for title to a yii widget
Posted: Feb 24, 2012 15:02;
Last Modified: May 23, 2012 18:05

The Yii file view.php
by default uses the [zii.widgets.CDetailView
] to display all examples of a given model. In the standard scaffolding produced by the gii
utility, this widget consists of references to attributes of the model without any further information (e.g. attribute names and the like):
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'editorialInstance_id',
'journal.shortTitle', // a reference to a relational attribute
'type',
'LastNamesFirstName', // a reference to a compound attribute
),
In this minimalist form, yii will calculate an appropriate label for the attribute on the basis of the attribute name: so, for example, in this case, editorialInstance_id will be appear in the view labelled “Editorial Instance” because Yii understands camelCase naming conventions and knows to strip off _id (it’s that good!).
A problem with this, however, is that we also provide customised label names as part of the attributeLabels()
method in our Model controller. Since that method allows arbitrary names, and since CDetailView
attempts to calculate labels on the basis of the attribute name, it is highly likely that the labels for different attributes will get out of synch in different places in your site. To give an example: in this particular case, the model for editorialInstance might have defined the label for editorialInstance_id as “ID” rather than “Editorial Instance”: since CDetailView
doesn’t check to see what you had on attributeLabels()
in the model class, switching from an edit view to an index will mean that the label of the attribute switches.
Ideally what you want to do is keep CDetailView
and attributeLabels()
in synch automatically. I’m sure there must be a way of doing this. In the meantime, however, here’s how you can set an arbitrary label in the widget (I’ve used it on the first and last):
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
array('value' =>
$model->editorialInstance_id, 'label' => 'ID'),
'journal.shortTitle',
'type',
array('value' => $model->person->lastNameFirstNames, 'label' => 'Person'),
),
