I have three rows in my yii CGridview and having two type of user logins.I have to hide one of the three rows depending on the type of user.Please help.
in your model add public static method
for example:
class Post extends CActiveRecord {
public function tableName() {
return 'posts';
}
public function rules() {
return array();
}
public function attributeLabels() {
return array();
}
...
public static function rulesUser() {
if ( Yii::app()->user->id = 1 ) {
return True;
} else {
return False;
}
}
add in your gridview for row:
$this->widget('zii.widgets.grid.CGridView',
array(
'id' => 'posts-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'emptyText' => '',
'columns' => array(
'id',
'title',
'post',
'date_create',
array(
'name' => 'status',
'visible' => Posts::rulesUser(),
)
array(
'class' => 'CButtonColumn',
),
),
)
);
Use the Conditional statements like this:
public function newsearch()
{
$id= Yii::app()->user->id;
if($id = Your conditon){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
Your Criteria to display
} else {
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
Your Criteria to display
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
And Call this dataprovider in your GridView data provider.
or add in model
public $visible = True;
in you search add if or switch
public function search()
{
$criteria = new CDbCriteria;
...
if (any if){
$this->visible = 1;
}
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
in your gridview
...
array(
'name' => 'value',
'visible'=>$model->visible,
)
...
I have solved the task by conditioning the CDbCriteria as follows,
if(!Yii::app()->session["admin"])
{
$criteria->condition='t.unique_id!="i-8874c6e3"';
}
Thanks all.
Related
I have to check no of products quantity from other table and display it in current grid view. So i created one function in model for getting count of that column.
But my question is How can I sort that custom column (checked_kpl) in grid view.
Here is my code.
MODEL
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('purchase_order_id', $this->purchase_order_id);
$criteria->compare('product_id', $this->product_id);
$criteria->compare('unit_price', $this->unit_price, true);
$criteria->compare('qty', $this->qty, true);
$criteria->compare('cost_price', $this->cost_price, true);
$criteria->compare('customs_percent', $this->customs_percent, true);
$criteria->compare('discount_percent', $this->discount_percent, true);
$criteria->compare('notes', $this->notes, true);
$criteria->compare('created_at', $this->created_at, true);
$criteria->compare('updated_at', $this->updated_at, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
public function getCheckedKpl() {
$checked_kpl = 0;
if (!empty($this->purchaseOrderArrivals)) {
foreach ($this->purchaseOrderArrivals as $eachArrival) {
$checked_kpl += $eachArrival->qty;
}
}
return $checked_kpl;
}
NOTE:
- purchaseOrderArrivals is another model. I already set relation with this model.
- getCheckedKpl function is giving me count of product quantity.
VIEW - In view I put this code in gridview widget for display column.
array(
'name' => 'checked_kpl',
'value' => '$data->getCheckedKpl()',
'type' => 'raw',
'class' => 'DataColumn'
),
Any help will appreciate. thanks.
class Model extends CActiveRecord {
// Adding attribute to work with SQL query
public $checked_kpl;
public function attributeLabels(){
// Prettify column name
return array( 'checked_kpl' => 'Checked kpl' );
}
public function search() {
$criteria = new CDbCriteria;
// Count subquery like this
// SELECT COUNT(*) as checked_kpl, id FROM {{table}} GROUP BY param
// Condition like this
// ( q1.id=t.id )
$criteria->join = "LEFT JOIN (/* **HERE IS A COUNT SUBQUERY** */) as q1 ON(/* **HERE IS A CONDITION** */)";
$criteria->select = array( '*', new CDbExpression("q1.checked_kpl as checked_kpl") );
// ... your criteria here
// Adding custom sort data
$sort = new CSort();
$sort->attributes = array(
'checked_kpl' => array(
'asc' => 'q1.checked_kpl',
'desc' => 'q1.checked_kpl DESC'
)
);
return new CActiveDataProvider( $this, array(
'criteria' => $criteria,
'sort' => $sort,
) );
}
}
How to install the scenario in yii ActiveRecord DadaProvider for each model;By default should update
public function afterFind()
{
if ($this->scenario === 'searchView') {
//myCode
}
parent::afterFind();
}
public function searchView()
{
$criteria = new CDbCriteria([
'select' => 'last_comment,name,data,cdate,mdate,edate,coperator,eoperator',
'with' => ['houses', 'houses.city', 'houses.district', 'houses.street', 'stages']
]);
return new CActiveDataProvider($this, [
'criteria' => $criteria,
'sort' => [
'defaultOrder' => 'cdate DESC'
]);
}
I don't think you can specify the scenario you want when fetching object the closest thing that I see is extending the CActiveDataProvider :
class CustomActiveDataProvider extends CActiveDataProvider {
protected function fetchData()
{
$data = parent::fetchData();
foreach($data as $model) {
// do what you wanted to do in the afterFind
// $model->afterFindSearchView()
}
return $data;
}
}
Then in your model you create the method afterFindSearchView, and when calling the data provider you do
return new CustomActiveDataProvider($this, [
'criteria' => $criteria,
'sort' => [
'defaultOrder' => 'cdate DESC'
]);
Yii 1.1.14
I have an employee table and a comment table
In the employee view i want to show all the comments of the employee in a grid (after the employee fields)
I tried to follow the example here
Here is where I am :
New code to defining a new search function in the model :
public $commentdate_param;
public $commentobservation_param;
...
public function rules()
{
return array(
...
array('public $commentdate_param, commentobservation_param, ...', 'safe', 'on'=>'search, searchIncludingComments'),
);
public function relations()
{
return array(
...
'employeecomments' => array(self::HAS_MANY, 'Employeecomments', 'employee_id'),
);
}
public function searchIncludingComments($parentID)
{
$criteria=new CDbCriteria;
$criteria->with=array('employeecomments');
$criteria->together = true;
$criteria->compare('t.employee_id',$parentID,false);
$criteria->compare('employeecomments.date', $this->commentdate_param,true);
$criteria->compare('employeecomments.observation', $this->commentobservation_param,true);
$sort = new CSort;
$sort->attributes = array(
'commentdate_param' => array(
'asc' => 'date_desc',
'desc' => 'date_desc DESC',
), '*', /* Treat all other columns normally */
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort,
));
}
Adding code in the controller actionView :
...
$child_model = new Employee("searchIncludingComments");
$child_model->unsetAttributes();
$this->render('view',array(
'model'=>$this->loadModel($id),
'child_model'=>$child_model,
'parentID' => $id
));
Adding code in the view :
...
<h3>Comments</h3>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'child-grid',
'dataProvider'=>$child_model->searchIncludingComments($parentID),
'filter'=>$child_model,
'columns'=>array(
'date',
'observation',
array(
'class'=>'CButtonColumn',
),
),
));
...
But I must have missed something because when it comes to grid rendering stops - but no error in log
Would be nice if somebody could help me !
I found the solution for me - all my changes and controler are worthless because now I do all stuff in the view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'comments-grid',
'dataProvider'=>new CActiveDataProvider('Employeecomment',array(
'criteria'=>array(
'condition'=>'employee_id=:eid',
'params'=>array(':eid'=>$model->id),
),
'sort'=>array(
'defaultOrder'=>'date DESC',
),
)),
Sure there is something I did not understand why it did not work ...
i want to build a update profile page, and i have 2 mysql table 1. account 2. info, so i want to insert or update the user info table.
this is what i have so far, but its return an error when saving the data "Call to undefined method stdClass::save()"
controller:
public function actionUpdate_profile() {
$model = new UserProfileForm();
$user = Userinfo::model()->findByPk(Yii::app()->user->id);
$model->name = $user->_name;
$model->myurl = $user->myurl;
if (isset($_POST['UserProfileForm'])) {
$model->attributes = $_POST['UserProfileForm'];
if ($model->validate()) {
$model->attributes = $_POST['UserProfileForm'];
$user->name = $model->name;
$user->myurl = $model->myurl;
$user->save();
});
});
$this->render('update_profile', array(
'model' => $model,
'user' => $user,
));
}
model:
class Userinfo extends CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return 'user_info';
}
public function rules() {
return array(
array('email, name, myurl', 'length', 'max' => 255),
);
}
public function attributeLabels() {
return array(
'user_id' => Yii::t('yii', 'Id User'),
'name' => Yii::t('yii', 'First Name'),
'myurl' => Yii::t('yii', 'Last Name'),
);
}
class UserProfileForm extends CFormModel {
public $name;
public $myurl;
public function rules() {
return array(
array('name, myurl', 'required'),
);
}
public function attributeLabels() {
return array(
'name' => Yii::t('yii', 'First Name'),
'myurl' => Yii::t('yii', 'Last Name'),
);
}
}
I know this not best practice but it is concise -
if($user = Userinfo::model()->findByPk(Yii::app()->user->id)) {
//then work with $user here
}
I have a problem that i can`t get throught.
I am trying to display information from relational table like this:
$dataProvider = PartnerSite::model()->with('siteCommercials')->findAll("user_id=" . Yii::app()->user->id);
$this->render('index', array(
'dataProvider' => $dataProvider,
'allMoney' => 1
));
But in my view i am seeing that error:
Relation "siteCommercials" is not defined in active record class "PartnerSite".
But the fact is that my model have relation:
public function relations() {
return array(
'goesFromSites' => array(self::HAS_MANY, 'GoesFromSite', 'site_id'),
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'siteCommercials' => array(self::HAS_MANY, 'SiteCommercial', 'site_id'),
);
}
So my question is. Is there is something wrong? I can't get it... In only one that model is a lot of problems... BeforeSave() doesn't work and relations work not well. User relation is working just fine.
Full listing of "model":
<?php
abstract class BasePartnerSite extends GxActiveRecord {
public $siteCommercials = "oke";
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{partner_site}}';
}
public static function label($n = 1) {
return Yii::t('app', 'PartnerSite|PartnerSites', $n);
}
public static function representingColumn() {
return 'site_name';
}
public function rules() {
return array(
array('site_name', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('site_name', 'length', 'max'=>255),
array('id, site_name, user_id', 'safe', 'on'=>'search'),
);
}
public function relations() {
return array(
'goesFromSites' => array(self::HAS_MANY, 'GoesFromSite', 'site_id'),
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'siteCommercials' => array(self::HAS_MANY, 'SiteCommercial', 'site_id'),
);
}
public function pivotModels() {
return array(
);
}
public function attributeLabels() {
return array(
'id' => Yii::t('app', 'ID'),
'site_name' => Yii::t('app', 'Site Name'),
'user_id' => null,
'goesFromSites' => null,
'user' => null,
'siteCommercials' => null,
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('site_name', $this->site_name, true);
$criteria->compare('user_id', $this->user_id);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
Your class is BasePartnerSite. In this class you define relation siteCommercials.
Your error message: "Relation "siteCommercials" is not defined in active record class "PartnerSite".
So then shouldn't your code be?
$dataProvider = BasePartnerSite::model()->with('siteCommercials')->findAll("user_id=" . Yii::app()->user->id);