Hi All,
I have used bootstrap.widgets.TbGridView in my admin view to display records. I have model SALE join with CLIENT model. I can sale with client id and as well as without client id. When I sale without client id and navigate to admin view, then i get "Trying to get property of non-object", it because there is no such client in CLIENT model with client id 0. Following is the VIEW and model code:
VIEW (admin.php)
$this->widget(‘bootstrap.widgets.TbGridView’, array(
'type'=>'striped bordered condensed',
'dataProvider'=>$model->search(),
'template'=>"{items}{pager}",
'filter'=>$model,
'columns'=>array(
'sale_date',
array('name'=>'client_id',
'value' => 'Clients::model()->find($data->client_id)->name'
),
'discount',
'remarks',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'htmlOptions'=>array('style'=>'width: 50px'),
//'template'=>'{view}{update}{delete}',
'buttons'=>array(
'delete' => array(
//'url'=>'Yii::app()->controller->createUrl("ports/delete", array("id"=>$data[id],"command"=>"delete"))',
'visible'=>'false'
),
),
),
),
));
MODEL CODE (sale model)
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('sale_date, productid, sale_quantity, sale_price', 'required'),
array('status', 'numerical', 'integerOnly'=>true),
array('stock_quantity', 'numerical'),
array('client_id, user_id', 'length', 'max'=>20),
array('discount', 'length', 'max'=>18),
array('remarks', 'length', 'max'=>200),
array('edt', 'safe'),
array('sale_quantity', 'validateQty'),
array('sale_quantity','compare','compareValue'=>'0','operator'=>'>','message'=>'Quantity must be greater than 0'),
array('sale_price','compare','compareValue'=>'0','operator'=>'>','message'=>'Sale price must be greater than 0'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, sale_date, client_id, discount, remarks, user_id, status, sdt, edt', 'safe', 'on'=>'search'),
);
}
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('sale_date',$this->sale_date,true);
$criteria->compare('client_id',$this->client_id,true);
$criteria->compare('discount',$this->discount,true);
$criteria->compare('remarks',$this->remarks,true);
$criteria->compare('user_id',$this->user_id,true);
$criteria->compare('status',$this->status);
$criteria->addCondition('edt is null');
return new CActiveDataProvider($this, array(
'criteria'=>$criteria
));
}