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.
Related
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
I would like to use the same model "Work" for handling similar CRUD operations for "Services" and "Markets" pages. There is a column in the Work mysql table called "category". If category is "service", then I would like the Index to show list of Services that are stored in the Work table. Similarly for the "Markets" page.
I would need two urls for the same Model (in the menu and create/ update operations etc.). How can I set this up in the URL Manager?
'services/create/' => 'work/create?&category="services"',
You would do it like this:
array(
//put it first so it has highest priority
'services/create' => 'work/create/category/services',
//other rules follow
)
Then your function in your controller would look like this:
public function actionCreate($category) {
//your code
}
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 ...')); ?>
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
?>
I have a user submit a field to the database, it validates, and makes the entry. The primary key of this new row is auto-incremented.
The user then gets to another form where that newly created field is required.
Can anyone shed any light on this problem?
Thanks in advance!
Check out Model::getInsertID();
$this->getLastInsertID();
cakephp model docs
// Save the first form
$this->Ingredient->save($newData);
// Get the id of the record just saved
$newIngredientId = $this->Ingredient->id;
// Redirect to a new form
$this->redirect(array(
'controller' => 'dish',
'action' => 'add',
'?' => array('lastId' => $newIngredientId)
));
http://book.cakephp.org/view/1031/Saving-Your-Data,
http://book.cakephp.org/view/1442/link
If it's in the database, when you get to your next form just read it out again in your controller and set it to a view variable.
$this->set('lastid', $this->Model->read(null,$id));
Or if you need to search the database for the field use find
$this->set('lastid', $this->Model->find('first', array('conditions'=>array('username'=>'MyUserName')) , array('id')));
You could go around it by using mysql_insert_id()