how to handle an array as a field in CGridView - yii

I'm using CGridView but one of the field is an array ['xx' ,'yy' , 'zz',...]
How can I display that i searched for an answer but did not find
is it possible to use something like DropDownlist to display the values in the array

I have a static array special_offer in a model;
It will filter by the value; This is a drop down list example;
I replaced a field within the cgridview with a array as follows:
array(
'name' => 'special_offer',
'value' => 'Package::$special_offer[$data->special_offer]',
'filter' => Package::$special_offer,
),

In order to use dropDownList in a CGridView column, your array needs to be associative. I recommend that you create a method in your model which converts it into an associative array, something like:
public function getAssociativeArray()
{
$array = array('xx', 'yy', 'zz'); // or use an attribute value
return array_combine(array_values($array), $array);
}
Then in your CGridView, add this as a column, replacing the model/field names with your own:
array(
'name' => 'yourFieldName',
'type' => 'raw',
'value' => 'CHtml::activeDropDownList($data, "yourFieldName", YourModel::model()->associativeArray)',
),

Related

How to hide a column in yii

I want to hide a column in yii based on a condition. I have used a function in model. but visible functionality is not working in this column.
$this->widget('zii.widgets.grid.CGridview', array(
'id'=>'gridview',
'dataProvider'=>$dp,
'columns'=>array(
array(
'header' => 'Entries',
'value' => '$data->entry_name'
),
array(
'name' => 'value',
'value' => '$data->entry_name',
'visible'=>'$data->show()',
'type'=>'raw'
),
)
)
);
function in Model
public function show()
{
.........
return 1 or 0;
}
but it not working. Please help
I don't see a condition anywhere yet. You should use a comparison operator like return $this->attribute !== null; in your show function. Which returns true if attribute contains some value. Or if you want to do it on random you can return rand(0,1) == 1;.

Remove empty option from listData at CGridView filter

I would like to remove the empty or first option of list data value.
I have Bankaccount model and it has a list, so I need to prevent from users to select all.
I already selected the first one of that list as a default, but now the problem is the empty option that can let user to select all Bankaccount still exist, so how can I remove.
This is my code
array(
'name' => 'bank_account_id',
'type' => 'raw',
'value' => '$data->bankaccount->BankAccountName',
'filter' => Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
If you look at this method that generates filter, you will see that it always adds empty option when it gets an array on input. So, the only way to hide empty option is to generate selectbox manually:
'filter' => CHtml::activeDropDownList(Bank::model(), 'bank_account_id',
Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
),
Using CHtml::activeDropDownList will give you Bank[bank_account_id] in the selectbox name.
for the filter part,
'filter' => Chtml::dropdownList('Bank[bank_account_id]' , 'selectedDefault',
Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
),

yii CGridView dataprovider and filter

I know we can show a gridview with a model and it's search method and filter the results, but can we make a gridview with another dataprovider and another model like this and filter its results? Does filter needs to be a part of dataprovider?
$attr = Yii::app()->request->getParam($name);
$model = new User('search');
$model->unsetAttributes();
$model->setAttributes($attr);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $myDataProvider,
'filter' => $model,
'columns' => array(
array(
'name' => 'username',
'type' => 'raw',
'value' => 'CHtml::encode($data->username)'
),
array(
'name' => 'email',
'type' => 'raw',
),
),
));
The above code doesn't work and I need to add a filter on a previously made data provider.
Btw $attr has a valid data, but grid is not filtered.
$model doesn't affect $myDataProvider since the data provider is not obtained using this model.
$model->search() returns a CActiveDataProvider which contains a CDbCriteria instance. Different CDbCriteria can be combined using mergeWith(). So if you would like the data to be filtered using the values from the $model
...
$model->setAttributes($attr);
$newDataProvider=$model->search();
$myDataProvider->criteria->mergeWith($newDataProvider->criteria);
$this->widget('zii.widgets.grid.CGridView', array(
...
Filter does not need to be a part of dataprovider, but data provider needs to take the model into account, if you want to use it for filtering.
The way this is done by default is to create the data provider using search method on your model, which sets conditions of your data provider based on model values, like so:
'dataProvider' => $model->search()
There is nothing preventing you from creating different data provider, for example:
'dataProvider' => $model->createAnotherDataProvider()
And in your User model:
public function createAnotherDataProvider() {
{
// create your second data provider here
// with filtering based on model's attributes, e.g.:
$criteria = new CDbCriteria;
$criteria->compare('someAttribute', $this->someAttribute);
return new CActiveDataProvider('User', array(
'criteria' => $criteria,
));
}

How to customize zii cgrid coiumn which are not in my database

I am using zii.widgets.grid.CGridView.....i want to add new coulmn in this.which is not in my database.how to add this column so that it show in listing
Just add a new column and stablish the value by hand:
'columns'=>array(
'something',
array(
'header'=> 'Custom',
'type' => 'raw',
'value' => '"something"'
),
),
These two articles may help:
CGridView reference:
http://www.yiiframework.com/doc/api/1.1/CGridView
CGridView - render custom columns:
http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/

SugarCRM - Add leads with auto-incremented ID

I use the SOAP API to add new leads to SugarCRM. Additionally, I use a plugin to assign an auto-incremented lead ID whenever a new lead is created (http://www.sugarforge.org/projects/autoincrement/).
Now, the plugin works fine, if I create a new lead via frontend. But, if I use the SOAP API, the function from the module, which assigns the auto-increment ID to the lead, does not trigger.
I create the lead via
$module = 'Leads';
$params = array(
'session' => $session,
'module_name' => $module,
'name_value_list' => array(
array('name' => 'id', 'value' => ''),
//array('name' => 'int_lead_id_c', 'value' => ''),
array('name' => 'first_name', 'value' => $_POST["first_name"]),
array('name' => 'last_name', 'value' => $_POST["last_name"]),
array('name' => 'phone_home', 'value' => $_POST["phone"]),
array('name' => 'email1', 'value' => $_POST["email"]),
array('name' => 'assigned_user_id', 'value' => '1'),
)
);
//Create the Lead record
$lead_result = $soapclient->call('set_entry', $params);
The function in the module is this one:
class SugarFieldAutoincrement extends SugarFieldBase {
/**
* Override the SugarFieldBase::save() function to implement the logic to get the next autoincrement value
* and format the saved value based on the attributes defined for the field.
*
* #param SugarBean bean - the bean performing the save
* #param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
* #param string field - the name of the field
*/
public function save(&$bean, $params, $field, $properties, $prefix = '') {
}
}
How can I make sure, that this function is also triggered, when adding leads via SOAP API?
Thanks a lot for your help! :-)
David
You would need to set the field type to 'autoincrement' and the dbType to 'int' in the vardef record for the field.
If I'm not mistaken, the Database has a UUID() trigger on insert for most tables, so you should be able to completely remove the id field.
If you want to trigger the function before saving, you can use beforeSave logic hook.