Yii Cheat Sheet
I keep a reference to some simple but painful task if you forget a common or something. I am new to Yii and this took awhile for me to figure out. Everything I read said do this or do that and not a single one worked for me. However, the following did.
Please note I'm using Yii 1.1.12.
The first part is if your models are NOT related.
Drop down with alphanumeric display for a numeric input
Say your dropdown field is binary and it inputs only numbers and you wanted to dispaly say a Yes, No, Male, Female, Active, Inactive etc. do the following.
model/_form
for example change 'gender', to:
echo $form->dropDownList($form, 'gender', array('1' => 'Male', '2' => 'Female'));
Same as above but adds an 'prompt' field at the top of your dropdown list if no value was selected.
echo $form->dropDownList($form, 'gender', array('1' => 'Male', '2' => 'Female', 'prompt' => 'Select Gender'));
The problem we have now is that it's going to disply 1 or 2 in your /view, /index and /admin tables. To change it to the alphanumeric value do the following:
Note:'gender' is my model name and you would be replacing 'gender', in the respected files listed below with the arrays that are also listed below.
model/view
array('name'=>'sex', 'type'=>'raw', 'value'=>$model->gender? "Male": "Female"),
(Note: Notice the 'type'=>'raw', this allows you to manipulate it to whatever you need. Also, it is going to assign the value in the numeric order. I.e. 1 = Male, 2 = Female, 3, 4, 5. So make sure you are consistant in your ording in your dropdown and views)
model/index
NOTE: if you use 'filter'=> in your index you must have a 'name' column or it will remove the search box from the top. If you use filter or just want to be simple use the same for admin and index.
array('header'=>'Gender','type'=>'raw', 'value'=>'$data->gender? "Male": "Female"'),
model/admin or index
array('name'=>'gender', 'header'=>'Gender', 'value'=>'$data->gender? "Male": "Female"'),
actual model file under protected/models
search:
please see the bottom for this part! I can't get the search to search name instead of number.
This part is if your models ARE related.
FOR THE FOLLOWING EXAPLES PLEASE NOTE: The easiest and fastest way to define relations is to add foreign key relationships before you create anything with gii. If you do so gii will generate the relations you.
My models are Contacts and Companies. I have relations defined between them both in their models. My Companies table (which is where the company comes from and in my database I have the name column as name. My Contacts table (which is where the firstname and lastname come from in my database my first name column is firstname and my lastname column is lastname)
Dropdown.
echo $form->labelEx($model,'your_model'); <?php echo $form->dropDownList($model, 'your_model', CHtml::listData( Yourrelatingmodel::model()->findAll(), 'primary_key_of_relating_model', 'name'),array('class'=>'span5'),array('prompt' => 'Select a Company')); <?php echo $form->error($model,'your_model');
so here is a real exaple:
echo $form->labelEx($model,'companyid'); <?php echo $form->dropDownList($model, 'companyid', CHtml::listData( Companies::model()->findAll(), 'companyid', 'name'),array('class'=>'span5', 'empty' => 'Select a Company')); <?php echo $form->error($model,'companyid');
File upload (dosen't have to be related)
echo $form->labelEx($model,'your_database_column_name_thats_in_your_model'); <?php echo $form->fileField($model,'your_database_column_name_thats_in_your_model',array('rows'=>6, 'cols'=>50, 'class'=>'span5')); <?php echo $form->error($model,'your_database_column_name_thats_in_your_model');
Join two column name together in breadcrumbs/whatever
I have columns firstname, lastname, and company that i want to show in my breadcrumbs so I did the following.
$model->firstname.' '.$model->lastname.' | '.$model->company,
So my complete breadcrumbs look like this
$this->breadcrumbs=array( 'Products'=>array('index'), $model->contacts=>array('view','id'=>$model->contactid), $model->firstname.' '.$model->lastname.' | '.$model->company, );
this outputs: Home>Contacts>John Doe | ABC Inc.
You can put whatever text between the ' ' . If you notice I chose to put 1 space between the ' ' to have a space between first and last names. Also a | between the last two models. You can also add text before/after by adding 'yourtext', before/after the line i added above.
Headings/titles:
Then as my title I put.
<h3><?php echo $model->firstname; <?php echo $model->lastname; </h3>
This outputs John Doe as the title.
Display name instead of number in potected/modelname/
NOTE IN ALL OF THE FOLLOWING EXAMPLES I REPLACED 'companyid', with the following arrays to show my name (the title of my name field) instead of the number.
/view
array('label'=>'Company', 'value'=>$model->company->name),
/admin
array('name'=>'companyid', 'header'=>'Company', 'value'=>'$data->company->name'),
/index
array('name'=>'companyid', 'header'=>'Company', 'value'=>'$data->company->name'),
/search: Someone please help me!!!! I can't figure it out.
The one problem that rises from all of this that I am unable to figure out is how to change the search portion. I do not know what to put in my model under the search section to make this work properly. As of now, say you changed companies from 1 to show ABC Inc. you still have to search 1 to find it. I assume I will have to do two differnt things for the transferring of numeric to alphanumeric as I showed in the first portion
vs.
The relations that are listed just above this. I don't know but someone please help!!!.
I tried this already but without luck:
$criteria->compare('t.id',$this->id); $criteria->compare('company_relation.name',$this->company_id, true); $criteria->with=array('company_relation');
I got some of this from: This link however, I had to change a few things to get it to work for me. So, if my version doesn't work then try those.
Good luck and let me know if this works for you!
BONUS:
Add this to your grid view above columns and it will make the whole row clickable. Note: change products to your model name.
'htmlOptions'=>array('style'=>'cursor: pointer;'), 'selectionChanged'=>"function(id){window.location='" . Yii::app()->urlManager->createUrl('products/view', array('id'=>'')) . "' + $.fn.yiiGridView.getSelection(id);}",