This is just a reminder to myself about setting up a Yii install. There are much more detailed examples on the web.
view.phpby default uses the [
zii.widgets.CDetailView] to display all examples of a given model. In the standard scaffolding produced by the
giiutility, this widget consists of references to attributes of the model without any further information (e.g. attribute names and the like):
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
<?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 ),
attributeLabels()method in our Model controller. Since that method allows arbitrary names, and since
CDetailViewattempts 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
CDetailViewdoesn’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. Read the rest of this entry »
firstNames. Elsewhere in your website, you want to refer to the underlying record in this table using the person’s whole name as a single entity (e.g. to provide a link, for example:
<a href="http://example.com/index.php?r=Person/view&id=1">Jane Q. Public</a>. Sometimes, you might be able to refer to the two attributes separately. For example, if you simply wanted to echo the content in a view somewhere you could use code like this:
This is a little inefficient if you are doing it a lot throughout your site, because you need to keep re-entering the same code correctly over and over again (and avoiding this is a main reason for going with an MVC framework like Yii in the first place). But the real trouble comes if you want to use the attributes in a context where the method you are invoking expects a single attribute—as is the case, for example, with the yii linking method CHtml::link. The way round this is to create a new compound attribute in your model. This means taking the two underlying attributes in your table and combining them into a single attribute already arranged the way you want that you can then invoke in other methods. Read the rest of this entry »
<?php echo CHtml::encode($data->person->firstNames) . ' ' . CHtml::encode($data->person->lastName); ?>
In the controllers established by gii, yii’s scaffolding tool, there is a standard method called accessRules() that defines what users can do what actions.