How do I apply a CDataColumn filter to my CGridView so it displays only rows with a null-value? - yii

I have a model with a boolean value, generated from a table like this:
CREATE TABLE receivable (
...
is_paid INTEGER DEFAULT NULL,
...
)
You should only take notice of the possible NULL value.
I have a gii-generated Receivable.php-model and a simple CGridView, like this:
$dataProvider = $model->search();
$dataProvider->pagination = ['pageSize'=>20];
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'filter'=>$model,
'columns'=>array(
'id',
[
'name'=>'is_paid',
'type'=>'raw',
'value'=>'($data->is_paid==1)?"PAID":"";',
'filter'=>['1'=>'PAID', '0'=>'0']
],
'someothercolumn',
['class'=>'CButtonColumn']
),
);
It should make sense so far? It does work fine I must say, with just one tiny problem - I want to allow filtering on null values as well!
'filter'=>['1'=>'PAID', '0'=>'0', null=>'null'] // This shows all records.
'filter'=>['1'=>'PAID', '0'=>'0', ''=>'null'] // This also shows all records.
'filter'=>['1'=>'PAID', '<>1'=>'null or zero'] // This shows 0-records only.
Well, now I'm at a loss. Is there any way I can use the CDataColumn.filter to allow the user to filter on null values? (Only display rows where 'is_paid'==null)
Edit: Values can be 1,0 or NULL, but the filter can only be applied for 1 or 0 (or show everything). How can I let the user display rows with null-values only?
Any help is much appreciated!

this is one way you can do it
1.'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
or something like this
2.is_paid:boolean
or something like this
3.'filter' => CHtml::listData(UserRegistry::model()->findAll(), 'id_user_registry', 'firstname' ),
In the above example i have the values in a db table
or something like this
4.'filter' => Lookup::items('option'),
and for the above example in the model you would have something like this
4. public static function items($type, $code)
{
if(!isset(self::$_items[$type]))
self::loadItems($type);
return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;
}
private static function loadItems($type)
{
self::$_items[$type]=array();
$models=self::model()->findAll(array(
'condition'=>'type=:type',
'params'=>array(':type'=>$type),
//'order'=>'position',
));
foreach($models as $model)
self::$_items[$type][$model->code]=$model->name;
}

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;.

Active Record- How to select a column with relationship?

Projects can have unlimited number of columns (to form a table or something), relationship MANY to MANY. To implement this tbl_project_rel_column is created. It stores project_id, column_id AND pos position of column in Project table.
I am using AC database approach. I have 2 models Project and Column.
Project model's relations method:
public function relations(){
return array(
...
'columns'=>array(self::MANY_MANY,'Column','tbl_project_rel_column('p_id','c_id')
);
}
Now can get all project's columns using something like this:
$model = Project::model()->findbyPk($p_id);
$columns = $model->columns;
But column doesn't store 'pos'(position) value of it's certain project.
How to get 'pos' value of tpl_project_rel_column table of certain project and certain column?
You can use the through feature instead of MANY_MANY. It may also be useful to index the results by the position column. Try something like this:
public function relations()
{
return array(
'projectColumns' => array(self::HAS_MANY, 'ProjectRelColumn', 'p_id', 'index'=>'position'),
'columns' => array(self::HAS_MANY, 'Column', 'c_id', 'through'=>'projectColumns'),
}
Now you can query for projects with columns like this:
$projects = Project::model()->with('columns')->findAll();
foreach($projects as $project) {
// projectColumns are indexed by position. You can sort by this now:
ksort($project->projectColumns)
foreach($project->projectColumns as $pos => $projectColumn)
echo "Pos: $pos Column: {$projectColumn->column->name}";
}

How can I display a single record in 2 rows by using Yii CGridView?

I am using CGridView in Yii, how can I display a single record in 2 lines?
Basically I want to show a record details in 1st row of table and on other row I want to display its summary, I tried it with div and css but can't get proper results, is anyone there who can help me in this case?
I am using like this:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bidding-grid',
'itemsCssClass' => 'data-default',
'dataProvider'=>$model,
'summaryText' => '',
'columns'=>array(
'people_detail_for_bid.Person' => array(
'type'=>'raw',
'name'=>'people_detail_for_bid.Person',
'value'=>'Yii::app()->Controller->createUserNameLink($data->people_detail_for_bid->PeopleFirstName." ".$data->people_detail_for_bid->PeopleLastName, $data->people_detail_for_bid->PeopleId).
"<br><span class=decriptionText>".$data->people_detail_for_bid->PeopleDesignation."</span>".
"<br><span class=decriptionText>".$data->people_detail_for_bid->PeopleEmail."</span>"',
'htmlOptions'=>array('width'=>200),
),
'timeAgo' => array(
'type'=>'raw',
'name'=>'timeAgo',
'value'=>'"<span class=decriptionText>".Yii::app()->Controller->_ago($data->PBPostedOn)."</sapn>"',
'htmlOptions'=>array('width'=>150),
),
),
));
?>
I think the best and cleanest way to accomplish this is to create a new extension and extend it to CGridView, override the renderTableRow function like this:
/**
* Renders a table body row.
* #param integer $row the row number (zero-based).
*/
public function renderTableRow($row)
{
if($this->rowCssClassExpression!==null)
{
$data=$this->dataProvider->data[$row];
echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'">';
}
else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
echo '<tr class="'.$this->rowCssClass[$row%$n].'">';
else
echo '<tr>';
$colCtr = 0;
foreach($this->columns as $column){
$column->renderDataCell($row);
$colCtr++;
}
echo "</tr>\n";
echo "<tr><td colspan=\"$colCtr\">This is the summary row. </td></tr>";
}
You can customize what you are rendering in the columns, so if you want to show two different fields of your table in the same row, you have to create a function in your model:
public function customColumn()
{
return $this->PeopleDesignation.'<br/>'.$this->PeopleEmail;
}
And then assign the method to the value of your column:
array(
'type'=>'html',
'value'=>'$data->customColumn()'
),
Cheers, Pablo.
After too much search .. and I tried with different ways now finally got a solution for this .. that solution is basically a kind of 2nd way to do anything not actual way .. but it works to me ..
.....
.....
'timeAgo' => array(
'type'=>'raw',
'name'=>'timeAgo',
'value'=>'"<span class=decriptionText>".Yii::app()->Controller->_ago($data->PBPostedOn)."</sapn></td></tr><tr><td colspan=\"6\"><span class=decriptionText>".$data->PBSummary."</span>"',
'htmlOptions'=>array('width'=>150),
),
.....
.....
added some table tags on last column to close row and added another one.
hope it works for all ..
thanks ..
Sounds like 2 interleaved CGridViews. So you might try that, but I can't imagine it will work out well:
Overlay one CGridView over the other, with enough transparent space in each row for the other to display it's row, and ...
a. one `CGridView` table offset vertically by half a row height, *OR*
b. one table's row's text vertically top-aligned & the other bottom-aligned
But somehow I doubt that's a CGridView capability ;) And keeping the two in sync so they pagenate and scroll together would be darn near impossible. Sounds like you need to enhance CGridView or derive a new widget from CGridView. I wonder if JQuery has something to do this?

CGridview and Yii Active Record Relation

I have two tables tbl_business and business_contacts of the following structure:
tbl_business
---
business_id (PK)
othercolumns
and
business_contacts
---
contact_id (PK)
business_id
othercolumns
The scenario is that one business row has many contacts. I am using cGridview using gii's CRUD generator and needed to display firstname and lastname from business_contacts (one of multiple possible rows in the table) for each tbl_business record.
As far as I understand, I've updated the relation function in tbl_business's model as:
'businesscontacts' => array(self::HAS_MANY,'BusinessContact','business_id','select' => 'contact_firstname, contact_lastname')
and for the same, a contact relation is defined in the business_contacts' model as:
'contactbusiness' => array(self::BELONGS_TO,'BusinessContact','business_id')
I expected that would work for pulling related records so that I can have something in the grid like, business_id, contact_firstname, contact_lastname , ... otherbusinesstablecolumns .. but I'm only getting blank values under firstname and lastname .. could someone please help me understand the error? :(
So you are trying to display a table of Businesses (tbl_business) using CGridView? And in each Business's row you want to list multiple Contacts (business_contacts)?
CGridView does not support displaying HAS_MANY relations by default. CGridView makes it easy to list which Business a Contact BELONGS_TO (i.e. you can use a column name like contactbusiness.business_id), but not all of the Contacts that are in a business.
You can do it yourself though, by customizing a CDataColumn. (Note: this will not allow you to sort and filter the column, just view. You'll have to do a lot more work in to get those working.)
First, in your Business model, add a method like this to print out all of the contacts:
public function contactsToString() {
$return = '';
foreach ($this->businesscontacts as $contact) {
$return .= $contact->contact_firstname.' '.$contact->contact_firstname.'<br />';
}
return $return;
}
(EDIT: Or do this to print out just the first contact):
public function contactsToString() {
if($firstContact = array_shift($this->businesscontacts)) {
return $firstContact->contact_firstname.' '.$firstContact->contact_firstname;
}
return '';
}
Then make a new column in your grid and fill it with this data like so:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'business-grid',
'dataProvider'=>$model->yourDataProviderFunction(),
'columns'=>
'business_id',
array(
'header'=>'Business Contacts', // give new column a header
'type'=>'HTML', // set it to manual HTML
'value'=>'$data->contactsToString()' // here is where you call the new function
),
// other columns
)); ?>
EDIT2: Yet another way of doing this, if you just want to print out ONE of a HAS_MANY relation, would be to set up a new (additional) HAS_ONE relation for the same table:
public function relations()
{
return array(
'businesscontacts' => array(self::HAS_MANY,'BusinessContact','business_id','select' => 'contact_firstname, contact_lastname') // original
'firstBusinesscontact' => array(self::HAS_ONE, 'BusinessContact', 'business_id'), // the new relation
);
}
Then, in your CGridView you can just set up a column like so:
'columns'=>array(
'firstBusinesscontact.contact_firstname',
),
Getting only the first contact could be achieved like this also:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'business-grid',
'dataProvider'=>$model->yourDataProviderFunction(),
'columns'=>
'business_id',
//....
array(
'name' => 'contacts.contact_firstname',
'value' => '$data->contacts[0]->contact_firstname', // <------------------------
'type' => 'raw'
);
//....
),

Zend_Db fetchAll() to return values as keys, not as key => value

Is there a way to change the default functionality in Zend_Db fetchall() method, so that it doesn't return this:
[0] => 100000055944231
[1] => 100000089064543
[2] => 100000145893011
[3] => 100000160760965
but this:
[100000055944231]
[100000089064543]
[100000145893011]
[100000160760965]
Although your question is actually flawed (noted by BartekR), I suppose you're trying to receive a simple array, instead of a multidimensional one.
You could do:
$results = $this->db->fetchAll($select);
$values = array_map(function($row) { return $row['column']; }, $results);
This will turn:
array(
array('column' => 'test'),
array('column' => 'test2'),
array('column' => 'test3'),
);
into
array(
'test',
'test2',
'test3'
);
(note; my example only works in PHP5.3+, if you're working with PHP5.2, you can define the function and use it by its name with array_map (e.g. array_map('methodName', $results))
I'm looking for a similar solution, I'm trying to load a field returned by the fetchAll($select) as the array key.. Without looping through the entire resultset.
So:
$results = $this->db->fetchAll($select, <FIELDNAME_TO_MAKE_KEY_OF_RESULTS_ARRAY>);
results[<my fieldname>]->dbfield2;
Further to Pieter's, I'd add the case where the rows are themselves arrays, and not just scalars; it's possible to nest the results, to as many fields as the query contains.
e.g. Here with two levels of nesting, respectively on field1 and field2.
$complex_results = array_map(function($row) { return array($row['field1'] => array($row['field2'] => $row)); }, $results);
As always, each row contains all fields, but $complex_results is indexed by field1, then field2 only.