Yii filter results without relation - php

I am using CGridView in Yii which is listing results with some of relations from other models
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'user.name',
'header' => Yii::t('app', 'User'),
'value' => '!isset($data->user->name) ? Yii::t("app", "Unknown user") : $data->user->name',
'type' => 'raw',
'filter' => CHtml::activeTextField($model, 'keyName'),
),
array(
'name' => 'visitor.company',
'header' => Yii::t('app', 'Company'),
'value' => 'isset($data->visit->visitor->company) ? $data->visit->visitor->company : (isset($data->rfid->rfid) ? Yii::t("app", "Company 1") : Yii::t("app", "Unknown company"))',
'type' => 'raw',
'filter' => CHtml::activeTextField($model, 'visitorCompany'),
),
array(
'name' => 'pass_id',
'header' => Yii::t('app', 'Pass'),
'value' => '!isset($data->pass_id) ? Yii::t("app", "Massing pass") : "<span class=\"label label-primary\">" . $data->pass_id . "</span>"',
'type' => 'raw',
),
),
)); ?>
and there is isset() condition which checks if relation exists (otherwise an error occurs)
But now I am looking for a way to filter those results which. For example to write "Unknown" in the filter to get the results without relations in the other table or "Company" to get those without some relation but with another existing relation

Related

Regarding Yii alignment of gridview

Is that possible to make alignment for grid view? As you see the picture above, the checkbox are out of the boxes. I think column should be need add something. Below is my part of the code:
'columns'=>array(
array(
'name' => 'id',
'class' => 'CCheckBoxColumn',
'selectableRows' => '99',
'checked' => 'true',
'htmlOptions'=>array('style'=>'text-align:center'),
),
array(
'name' => 'edesc',
'header' => 'School Type',
'headerHtmlOptions'=>array('style'=>'text-align:left')
),
you could try adding a proper style width for checkbox column (and for the edesc column)
array(
'name' => 'id',
'class' => 'CCheckBoxColumn',
'selectableRows' => '99',
'checked' => 'true',
'htmlOptions'=>array('style'=>'text-align:center; width: 10%;'),
),
array(
'name' => 'edesc',
'header' => 'School Type',
'headerHtmlOptions'=>array('style'=>'text-align:left; width: 90%;')
),

Yii view not work, EColumnsDialog

picture shows what I have and what they want to have
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value'=>'$data->id',
),
I have to do so that the "value => id" instead display an NO.ID Started to display an TXT_ ID NO.ID
By using createCommand
Yii::app()->db->createCommand('SELECT CONCAT('TXT ',id),other_fields.. FROM table WHERE 1');
This will definitely help you
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value' =>'$data->id',
'type' =>'raw',
"value" => function($data){
echo "TXT ".$data->id;
},
),

Yii: Can not Display Data in Grid View

I can't list data in grid using yii framework. My controller is Sitecontroller.php, My view is list_jobseeker.php.
I got the error:
Parse error: syntax error, unexpected '*', expecting ']' in C:\wamp\www\yii_new\framework\base\CComponent.php(612) : eval()'d code on line 1
Anybody give any suggestion to correct these issue?
My controller:
public function actionlist_jobseeker()
{
$session_id=Yii::app()->session['user_id'];
if ($session_id == "")
{
$this->redirect( array('/employee/site/login'));
}
$user_id =$session_id;
$items = Yii::app()->db->createCommand()
->select('*')
->from('job_seeker_profile s')
->join('job_profile j','s.user_id = j.user_id')
->order('s.id')
->queryAll();
$this->render('list_jobseeker',array('items' =>$items));
}
My view page - list_jobseeker.php
<h1>View Jobseeker</h1>
<div class="flash-success">
</div>
<div class="form">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'rates-phase-grid',
'htmlOptions' => array('class' => 'table table-striped table-bordered table-hover'),
'dataProvider'=>new CArrayDataProvider($items),
'columns' => array(
array(
'name' => 'Name',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->name)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
),
array(
'name' => 'Email',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->email)',
'htmlOptions' => array('style'=>'width:250px;','class'=>'zzz')
),
array(
'name' => 'Password',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->password)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz')
),
array(
'name' => 'Contact No',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->contact_no)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'name' => 'Gender',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->gender)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'class' =>'CButtonColumn',
'deleteConfirmation'=>'Are you sure you want to delte this item?',
'template'=>'{update}{delete}',
'buttons' =>array('update'=>array(
'label'=>'edit',
'url'=>'Yii::app()->controller->createUrl("UpdateJob",array("id"=>$data["id"]))',
),
'delete'=>array('label'=>'delete',
'url'=>'Yii::app()->controller->createUrl("DeleteJob",array("id"=>$data["id"]))'),
)
)
),
));
?>
</tbody>
change all the [*]
$data[*]->name
to match columns
$data["name"]

Yii Pager not working as expected

I am using the Yii Framework, and I'm having issues with the bootstrap.widgets.TbGridView widget in conjunction with the Pager widget.
(Please note that I am new to Yii - so feel free to point out any obvious mistakes on my behalf)
All the filtering works fine, however when I select any option from the 'Fulfilled' dropdown menu it changes the URL structure of the Pager.
For instance, by default when I view the Pager links in Firebug the URL looks like this:
3
However once I make a selection from the 'Fulfilled' dropdown, and try to use the Pager again it no longer works, and after inspecting it via Firebug the Pager links have all changed dramatically to this:
3
I would expect the URL to retain a very similar structure as the initial link above.
Below is the view code for the page
<div class="row-fluid">
<div class="inner">
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id' => 'shoppurchases-grid',
'type' => 'striped bordered condensed',
'dataProvider' => $model->setPurchaseType('school')->search(10),
'filter' => $model,
'template' => '{summary}{items}{pager}',
'columns' => array(
array(
'header' => 'Product Image',
'value' => 'CHtml::image($data->product->displayImageUrl, $data->product->product_name, array("class"=>"grid-image"))',
'type' => 'raw',
),
array(
'header' => 'Product',
'name' => 'product.product_name',
'value' => '$data->product->product_name',
'filter' => CHtml::activeTextField($model, 'filterProductName', array('placeholder' => 'filter by product name')),
'type' => 'raw',
),
array(
'header' => 'Username',
'name' => 'user.firstname',
'value' => '($data->user instanceof MyUser) ? CHtml::link(CHtml::encode($data->user->username),array("/carrot/myuser/update","id"=>$data->user->user_id)) : ""',
'filter' => CHtml::activeTextField($model, 'filterUserName', array('placeholder' => 'filter by username')),
'type' => 'raw'
),
array(
'header' => 'Form Name',
'name' => 'user.form_name',
'value' => '($data->user instanceof MyUser) ? $data->user->form_name : ""',
'filter' => CHtml::activeTextField($model, 'filterFormName', array('placeholder' => 'filter by form name')),
'type' => 'raw'
),
array(
'header' => 'Student',
'name' => 'user.username',
'value' => '($data->user instanceof MyUser) ? $data->user->fullname : ""',
'filter' => CHtml::activeTextField($model, 'filterFirstName', array('placeholder' => 'filter by first name')),
'type' => 'raw'
),
array(
'header' => 'Price',
'name' => 'price',
'filter' => BHtml::activeCompareableTextField($model, 'price', array('placeholder' => 'filter by price')),
'type' => 'raw'
),
array(
'header' => 'Purchase Time',
'name' => 'purchase_time',
'value' => 'app()->dateFormatter->format("dd/MM/y H:m:s", $data->purchase_time)',
'filter' => BHtml::activeCompareableDateRange($model, 'purchase_time', array('placeholder' => 'filter by purchase time')),
'type' => 'raw'
),
array(
'header' => 'Fulfilled',
'name' => 'fulfilled',
'value' => user()->hasAuth(Group::READ_ONLY, "equal") ? '$data->fulfilled ? "Yes" : "No"' : 'CHtml::activeCheckBox($data, "fulfilled")',
'filter' => CHtml::activeDropDownList($model, 'fulfilled', array(0 => 'No', 1 => 'Yes'), array('prompt' => 'All')),
'type' => 'raw',
'htmlOptions' => array('class' => 'align-center'),
'headerHtmlOptions' => array('class' => 'align-center'),
),
array(
'name' => 'organisation_name',
'visible' => ((user()->hasAuth(Group::GROUP_ADMIN, 'equal')) && (!user()->hasState('view_org'))),
'filter' => CHtml::activeDropDownList($model, 'organisation_id', CHtml::listData(Organisation::model()->leaChildren, 'organisation_id', 'organisation_name'), array('prompt'=>'All Schools')),
),
array(
'header' => '',
'value' => 'CHtml::link("Refund", Yii::app()->controller->createUrl("bulk",array("id"=>$data->primaryKey)), array("class" => "btn btn-save", "name" => CHtml::activeName($data,"refunded")))',
'type' => 'raw',
'visible' => !user()->hasAuth(Group::READ_ONLY, "equal")
),
),
)); ?>
</div>
Thanks in advance - please remember I'm new to Yii
update...
I've checked the Firebug log and can see the GET request is failing (as it shows up in RED text in the Firebug log)
The URL is :
http://mysite.local/site/shop/purchases/admin/ShopPurchases%5BfilterProductName%5D//ShopPurchases%5BfilterUserName%5D//ShopPurchases%5BfilterFormName%5D//ShopPurchases%5BfilterFirstName%5D//ShopPurchases%5Bprice%5D//ShopPurchases%5Bpurchase_time%5D//ShopPurchases%5Bfulfilled%5D/0/ajax/shoppurchases-grid/ShopPurchases_sort/price?ajax=shoppurchases-grid
Once it tries to call this 'failed' URL the Pager stops working until I do a page refresh (e.g F5) and it returns to its default pager settings.
update 2...
The failed URL when loaded directly into the address browser for some reason will automatically attempt to add 'www.' at the start of the URL so the full url now looks like this..
http://wwwmysite.local/site/shop/purchases/admin/ShopPurchases[filterProductName]//ShopPurchases[filterUserName]//ShopPurchases[filterFormName]//ShopPurchases[filterFirstName]//ShopPurchases[price]//ShopPurchases[purchase_time]//ShopPurchases[fulfilled]/0/ShopPurchases_page/2/ajax/shoppurchases-grid?ajax=shoppurchases-grid
Is this something possibly related to my local htaccess file (this problem doesn't seem to exist on our 'live' version of the app.
I had to use some functionality to 'Remove Filters' in Yii for this to work - will post some code shortly.

yii CGridView filter with relations

I'm using yii for my web application. In one of my view I have CGridView and dataprovider is Mail model. In this model I have relation with with 3 other models. In the grid I show cols from three models. How can I filter the CGridView?
UPDATE:
<?php $dialog = $this->widget('ext.ecolumns.EColumnsDialog', array(
'options'=>array(
'title' => 'Layout settings',
'autoOpen' => false,
'show' => 'fade',
'hide' => 'fade',
),
'htmlOptions' => array('style' => 'display: none'), //disable flush of dialog content
'ecolumns' => array(
'gridId' => 'mails-grid', //id of related grid
'storage' => 'session', //where to store settings: 'db', 'session', 'cookie'
'fixedLeft' => array('CCheckBoxColumn'), //fix checkbox to the left side
'model' => $dataprovider, //model is used to get attribute labels
'columns'=>array(
array(
'name'=>'mailTemplate.name',
'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
),
'sendDate',
array(
'name'=>'mailTemplate.subject',
'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
),
array(
'name'=>'client.email',
'filter'=>CHtml::activeTextField($dataprovider, 'client'),
),
array(
'name'=>'client.name',
'filter'=>CHtml::activeTextField($dataprovider, 'client'),
),
array(
'name'=>'operator.username',
'filter'=>CHtml::activeTextField($dataprovider, 'operator'),
),
array(
'name'=>'status',
'value'=>array('MailHelper', 'getEmailStatus'),
'filter'=> CHtml::activeDropDownList($dataprovider, 'status', Mail::getEmailStatuses()),
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}',
'buttons'=>array(
'update' => array(
'url'=>'$this->grid->controller->createUrl("/email/editTemplate", array("templateId"=>$data->id))',
),
),
)
),
)
));
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'mails-grid',
'dataProvider'=>$dataprovider->search(),
'columns' => $dialog->columns(),
'filter' => $dataprovider,
'template' => $dialog->link()."{summary}\n{items}\n{pager}",
)); ?>
I have Restaurant, City, Country and User models with relations between them.
Model:
public function search() {
$criteria=new CDbCriteria;
$criteria->together = true;
$criteria->with= array('xCountry','xCity','User');
$criteria->compare('Id',$this->Id,true);
$criteria->compare('Restaurant.Name',$this->Name,true);
$criteria->addSearchCondition('xCountry.Name',$this->Country);
$criteria->addSearchCondition('xCity.Name',$this->City);
$criteria->compare('Zip',$this->Zip,true);
$criteria->compare('Address',$this->Address,true);
$criteria->compare('Description',$this->Description,true);
$criteria->compare('Restaurant.Active',$this->Active,true);
$criteria->addSearchCondition('User.Username',$this->Owner);
$criteria->compare('Lat',$this->Lat);
$criteria->compare('Lon',$this->Lon);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'restaurant-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'Id',
'Name',
'Zip',
'Address',
'Active',
array(
'name' => 'User.Username',
'header' => 'Username',
'filter' => CHtml::activeTextField($model, 'Owner'),
'value' => '$data->User->Username',
),
array(
'name' => 'xCountry.Name',
'header' => 'Country',
'filter' => CHtml::activeTextField($model, 'Country'),
'value' => '$data->xCountry->Name',
),
array(
'name' => 'xCity.Name',
'header' => 'City',
'filter' => CHtml::activeTextField($model, 'City'),
'value' => '$data->xCity->Name',
),
array(
'class'=>'CButtonColumn',
),
),
));
I hope this can help you.
UPDATE:
What if you try something like this:
...
'columns'=>array(
'mailTemplate.name',
'sendDate',
'mailTemplate.subject',
'client.email',
...
UPDATE #2:
Prepare yourself this will be a bit dirty.
Let's say we've got two classes, A and B. B belongs to A.
B's got a property, let's say "color" and we want to display it in our grid where we list the "A"s.
The first thing you have to do is, manually create a property to your data provider class (what is "A").
This property will be "colorOfB", so you have to add "public $colorOfB;" to your model A.
Add criteria for this property:
$criteria->compare('B.color',$this->colorOfB,true);
Add the column to the grid:
array(
'name' => 'B.color',
'header' => 'Color of B',
'filter' => CHtml::activeTextField($model, 'colorOfB'),
'value' => '$data->B->color'),
The final thing is to set this property manually in A's controller:
$modelA = new A('search');
$modelA->colorOfB = $_GET['A']['colorOfB'];
this will set select list
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'avto-ugon-grid',
'dataProvider'=>$model_data,
'filter'=>$model,
'columns'=>array(
'id',
array(
'name' => 'time',
'value' => 'date("d/m/Y", $data->time)',
'type' => 'html',
),
array(
'name' => 'nomer',
'value' => '$data->nomer',
'type' => 'html',
),
array(
'name' => 'id_marka',
'value' => '$data->idMarka->mark',
'type' => 'html',
'filter'=> CHtml::listData(AvtoUgon::model()->with('idMarka')->findAll(array('group'=> 'id_marka', 'order'=> 'idMarka.mark')), 'id_marka', 'idMarka.mark'),
),
array(
'name' => 'id_model',
'value' => '$data->idModel->model',
'type' => 'html',
'filter'=> CHtml::listData(AvtoUgon::model()->with('idModel')->findAll(array('group'=> 'id_model', 'order'=> 'idModel.model')), 'id_model', 'idModel.model'),
),
array(
'name' => 'color',
'value' => $data->color,
'type' => 'raw',
),
array(
'name' => 'id_street',
'value' => '$data->idStreet->street',
'type' => 'html',
),
array(
'name' => 'publish',
'value' => 'CHtml::link($data->publish ? "Опубликовано" : "Не опубликовано", Yii::app()->controller->createUrl("publish", array("id" => $data->id)))',
'type' => 'html',
),
/*'id_street',
'nomer',
*/
array(
'class'=>'CButtonColumn',
),
),
));

Categories