Yii: How to populate a Select input with another model data? - yii

I'm playing around with a small app in order to learn to use Yii.
I've created a small webapp with 2 models / tables: Projects and tasks. (1-to-many relationship, properly configured in the model classes).
I'm now trying to customize the Task/create view, replacing the text input field with a select box proposing the list of available projects.
I opened the form view and tried this:
<div class="row">
<?php echo $form->labelEx($model,'project_id'); ?>
<?php echo $form->textField($model,'project_id'); ?>
<?php
// my hack starts here
$projects = Project::model()->findAll();
$list = CHtml::listData($projects, 'id', 'name');
echo $form->listBox($model,'project_id','', $list); ?>
// my hack ends here
<?php echo $form->error($model,'project_id'); ?>
</div>
But it keeps throwing warnings or error (such as Invalid argument supplied for foreach(), and definitely does not work. I'm failing to understand what i'm doing wrong. Can you help ?

Your arguments are not in order (it should be):
$frameworks = Framework::model()->findAll();
$list = CHtml::listData($frameworks, 'id', 'name');
echo $form->listBox($model,'framework_id', $list,array());
Check the documentation

OK, i found it, thanks to Larry Ullman excellent tutorial.
Here it is:
<?php echo $form->dropDownList($model,'project_id', CHtml::listData(Project::model()->findAll(), 'id', 'name')); ?>

Related

readonly textfield using scenarios yii

How can I disable textfield using scenarios in yii? I have 3 classes of accounts superadmin, admin and normal users. All 3 classes of users have access to update information about them but one of the field accountId can be updated by superadmin and admin only but that field should be displayed to users also. Currently I am doing it in the following way.
<div class="row">
<?php echo $form->labelEx($user,'accountID'); ?>
<?php
if(Yii::app()->user->checkAccess('admin'))
echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32));
else
echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32, 'disabled'=>'true'));?>
<?php echo $form->error($user,'accountID'); ?>
</div>
This method has solved my problem but it is not a good method and better method is using scenarios. How can I implement same using scenarios?
what i do is i create a function that checks if the user has access. This will lessen my code to make it easier to maintain.
echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32,checkAccess($userId)));?>
// my function
function checkHTMLUserAccess($userId){
// Some codes
if ($hasAccess) return array('disabled'=>true);
else return array();
}
something like that :)
<div class="row">
<?php echo $form->labelEx($user,'accountID'); ?>
<?php echo $form->textField($user,'accountID',array('size'=>32,'maxlength'=>32, 'disabled'=>Yii::app()->user->checkAccess('admin'))); ?>
<?php echo $form->error($user,'accountID'); ?>
</div>

Showing a specific attribute when generating CRUD in Yii web framwork using giix extenstion

I am generating Models and CRUD for my database tables using giix in YII web framework, the thing is I want to change some of the attributes that showed to me but I dont know how ? I get into the code _FORM.php of the generated CRUD to one of the table and I knew the piece of code that I must change it to get a different attribute instead of one that shown to me without knowing why ?
<div class="row">
<?php echo $form->labelEx($model,'idEmployee'); ?>
<?php echo $form->dropDownList($model, 'idEmployee', GxHtml::listDataEx(Employee::model()->findAllAttributes(null, true))); ?>
<?php echo $form->error($model,'idEmployee'); ?>
</div><!-- row -->
in the previous code the form showed a drop-down list from another table jointed with the current table according to idEmployee, he's showing an attribute that I dont want, I want to know how to render the FirstName and the LastName in the drop-down list, any help please ?
I believe it is easier when you just create your own dropdown list provider
in the Employee.php you add these two functions:
public function getFullName()
{
return $this->first_name.' '.$this->last_name; // or what ever you want to be shown on the drop list
}
public static function getNamesList() {
return CHtml::listData(self::model()->findAll(), 'idEmployee', 'fullName');
}
in the _FORM.php write:
<div class="row">
<?php echo $form->labelEx($model,'idEmployee'); ?>
<?php echo $form->dropDownList($model, 'idEmployee', Employee::getNamesList()); ?>
<?php echo $form->error($model,'idEmployee'); ?>
</div><!-- row -->

Passe $model data with renderPartial in Yii

I've been trying this for about 2 hours and nothing was changed.
I have a model Users, I wanted to pass its _form.php view to views/site/index.php like that :
<?php echo $this->renderPartial('/users/_form', array('model'=>Users::$model)); ?>
the result: the layout has gone, and the form doesn't displayed.
Any help guys ? and thanks in advance.
<?php echo $this->renderPartial('/users/_form', array('model'=>Users::$model)); ?>
should read
<?php $this->renderPartial('/users/_form', array('model'=>$model)); ?>
assuming you have instantiated $model already. If not
<?php $this->renderPartial('/users/_form', array('model'=>new User)); ?>
should work.

yii framework: how to i put a label to a radio button? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Yii CHtml::radioButtonList - CSS to align horizontally
Am new to yii framework and the doubt might be silly!
I want to write a radio button code with its label next to it!
My code is as follows
<div class="column">
<?php echo $form->radioButton($model,'radio',false); ?>
<?php echo $form->error($model,'radio'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'I need a room'); ?>
</div>
But it doesnt look nice! Is there any better way of doing this?
I got the code online! Sharing it of that it can be useful for someone in future
<?php echo $form->radioButtonList($model,'radio',array('m'=>'male','f'=>'female'),array('separator'=>'', 'labelOptions'=>array('style'=>'display:inline'))); ?>
The following code will create a label for the radiobutton created with the 'radio' attribute. Place this code after the 'echo $form->radiob.....'
<?php echo CHtml::activeLabel($model, 'radio', array('label' => 'My Label', 'style'=>'display:inline')); ?>
Use the display:inline or add a css class to get the label exactly where you want.

Multiplying values from a textbox in Yii

I'm new to Yii, and I'm trying to compute values from textboxes. I want the computation codes to be on the same form where the textbox is being displayed (since the product will also be shown on a textbox of the same form where they will input the factors). I've tried this guess, but it didn't work. How do I fix this problem?
<div class="row">
<?php echo $form->labelEx($model,'Quantity_In_Pieces'); ?>
<?php echo $form->textField($model,'Quantity_In_Pieces',array('Quantity_In_Pieces' => ('Quantity').val() * ('Hold').val());?>
<?php echo $form->error($model,'Quantity_In_Pieces'); ?>
</div>
seems like you are trying to write jQuery where there should be php code.
<?php echo $form->textField($model,'Quantity_In_Pieces',array('Quantity_In_Pieces' => $model->quantity * $model->hold);?>