Yii-CRUD: Getting select boxes filled with data from other tables - yii

Do I need some more work, to get a select box with the corresponding data (e.g. land list from an another db-table) in the created insert form (via CRUD) or it is enough to define the relations in the models and yii would do this for me automatically?

Since you haven't provided any code, let me show you with an example. Suppose we have a user table and a group table and need to select a group for a user which is selected with a select box.
In the user model you can have a function like
public function getGroupName()
{
return CHtml::listData(Group::model()->findAll();
}
In the form view of user create you can populate the select box like below:
<?php echo $form->dropDownListRow($User, 'group_id', $User->getGroupName(),array('prompt' => 'Select ...')); ?>

Related

yii cGridView multicomment

I'm not sure what is the way to do this , so here I ask:
I have a Person model and Event model, and a connection table Person_Event.
The interface that I got now works in the following way:
A person is logging in and his id is being send via URL
The person is selecting events he is interested in from the cGridView (checkbox column)
Writing some comment
4.Pressing send button , and the following create action is being triggered:
public function actionXcreate()
{
$model=new Person_Event;
if(isset($_POST['Person_Event']))
{
foreach ($_POST['selectedIds'] as $eventId)
{
$pmodel=new Person_Event;
$pmodel->person_id=$this->_person->id; //the id of the person who is logged in
$pmodel->attributes=$_POST['Person_Event']; //the comment
$pmodel->event_id = $eventId; //all the events he checked in the grid
if (!$pmodel->save()) print_r($pmodel->errors);
}
$this->redirect(array('site/success'));
}
So far , all is logical and simple. However , what I end up is that the comment the person wrote is being duplicated to every person_event row in the DB.
I want to put a text box in each row of the grid , and the commnet that will be written there will go to the specific event.
Now , I found this topic in yii about "admin-panel"
which is kind of helpful , BUT:
I already have a foreach in the action , the one that matches the person's id with the event's id , so how can I put another individual comment for each combo?
The default CGridView supports only basic functionality, you would need to extend CGridView or use an extension to make columns editable
Easiest way to do this is use something like TbEditableColumn from Yii-booster library
see http://yiibooster.clevertech.biz/extendedGridView#gridcolumns EditableColumn in the additional column types section
If you do not like or wish to use twitter-bootstrap styling a standalone extension like http://www.yiiframework.com/extension/eeditable will help.
Alternatively you can extend CGridView yourself to extend it to support column level editing

Yii link with [ as a parameter

With GII I have created a list of records. I use the admin view so they are in a table view. On top of the table it is the search with a status for the records. When the status dropdown is changed I submit the form and the table gets searched. I want the default view of the admin to show only the active records so I want to create a link in the menu to this:
medium/admin/?Medium[status]=active
The actual link of course is
medium/admin/?Medium%5Bstatus%5D=active
I have tried to do it with:
CHtml::link('Mediums', array("medium/admin", array('Medium[status]' => 'active')))
CHtml::link('Mediums', array("medium/admin", array('Medium%5Bstatus%5D' => 'active')))
CHtml::link('Mediums', array("medium/admin", array('Medium' => array('status' => 'active'))))
But all of the links are incorrect so the default view of the table is with all the records shown.
What is the correct way to create such a link?
Thank you.
http://www.yiiframework.com/doc/api/1.1/CHtml#link-detail and http://www.yiiframework.com/wiki/48/ will be usefull for you.
CHtml::link(CHtml::encode('Mediums'),array("medium/admin", "status"=>"active"));
Then ensure that in your controller you have something like this:
public function actionAdmin($status)
Now you ca use 'status' in your action.

Dropdown without model in yii

I am using a component for parsing a country api in yii. So in the form drop down list call the function for listing country. The function returned country list as array.
form.php
<?php echo $form->labelEx($model,'country'); ?>
<?php $cty= Country::getCountry();
echo $form->dropdownList($model,'country', $cty , array('style'=>'width: 175px','empty'=>array('empty'=>Yii::t('app','Select Country'))));?>
Now the country list loaded correctly in drop down, but when on saving time the corresponding id of country is saved. i want to save the country name in db.How it solved?
You have to build your own custom array with the needed keys/values, e.g. :
$cty = Country::getCountry();
$cty = array_combine(array_values($cty), $cty);
You can use this way (in case if you need all items of Country table)
$cty = CHtml::listData(Country::model()->findAll(), 'name', 'name');

Custom Form With Parts Linked To exisitng Model (ActiveRecord)

i'm madding a form (Yii framework) that does not represent a database table but it contains a part "mapped" to a database table (like a belong_to relation).
So you can consider a contact form in which you can choose to whom send the email with a dropdown list that represent users in database.
And below you can type your email subject and content.
So here the contact form is not stored in the database, so it is not an ActiveRecord instance, but it contains "relation" like an ActiveRecord has.
My question is : How do i build my form class ?
I want to be able to do this in the view :
$activeFormWidget->dropdown($form->user, 'name', User::model()->getUsers());
You can use the CHtml::dropdownList to build dropdown list without a CActiveForm:
<?php
echo CHtml::dropDownList('user', // the input name
'', // initial selected value
CHtml::listData(User::model()->getUsers(), 'id', 'name'), // your data
array()); // htmlOptions
?>

Model save mutiple records and validation rules

I have a case where user can select multiple values in the list box and save it to the database using model.
Here is the table structure
user_id int(11) , cars_id int(5)
Here is the snippet of my view
<?php echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20) );?>
<?php echo CHtml::dropDownList("targetCars", '', array(),array('size'=>20) );?>
User selects the cars from sourceCars and moves into targetCars using Jquery ( This part is done) and
clicks on Save or Submit button .
Now I should be able to save all the cars he/she selected in the targetCars list. Moreover in model I should put a condition that user can't save more than 10 cars and at least one car should be selected . Also user can select 5 cars at one time and next time when he comes he should be able to select max 5 cars only since he already save 10 records .
Could you please throw me some idea to implement this ? any Links that can guide me ?
your question is to limit selection of cars between 1-10.
You need validate user input both client and server.
At server,you can custom a ActiveRecord validation
public function rules()
{
return array(
array('cards_id', 'limitSelect','min'=>1,'max'=>10),
);
}
public function limitSelect($attribute,$params)
{
//and here your code to get the count of selection of cars for a user
...
if($count<=$params['min'])
$this->addError('cards_id','at least one car should be selected');
if($count>=$params['max'])
$this->addError('cards_id',' can't select more than 10 cars');
}
//and for mutiple select you can code this:
echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20,'multiple'=>true) );
//anyway you can implement it in several way
Sounds like you want to use scenarios, see docs here. You can dynamically set the scenario with CModel::setScenario based on the user flow.