How to define model parameter value in Yii? - php

I have a user table in which i have multiple roles and i am showing different user on different action by using same model search function. By this
public actionAdmin(){
$model = new User('search')
$model->unsetAttributes();
$model->userRole = UmsConfing::ADMIN;
if(isset($_GET('User')))
$model->attributes = $_GET['User'];
$this->render('userlist',arary('model'=>$model));
}
I am using this function for different roles .This works well. but now i want to show admin and operation user in same list i tried
$model->userRole = UmsConfig::ADMIN || UmsConfig:: OPERATION
but i didn't work Please help.

Please you can try this below code:
public actionAdmin(){
$model = new User('search')
$model->unsetAttributes();
if(isset($_GET('User')))
$model->attributes = $_GET['User'];
$this->render('userlist',arary('model'=>$model));
}
Below codeadd in User Model Search Function:
model/User.php
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);
$criteria->addInCondition('userRole', array (UmsConfig::ADMIN,UmsConfig:: OPERATION));
$criteria->compare('status',$this->status);
$criteria->compare('is_deleted',$this->is_deleted);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => yii::app()->params->pagesize,
),
'sort'=>array(
'defaultOrder'=>array(
'id'=>CSort::SORT_DESC
),
),
));
}

Related

Modify Data that is put into CGridView - Yii

I'm working on a project in Yii where I would like the user to be able to update information on his mobile "accounts". The database I use has multiple accounts for each person (and, for that matter, has multiple individuals who will use the system). Now, when the user goes to "manage his account" he is able to manage all of the accounts of the database (not just his own).
Currently the code in the controller looks like this
public function actionAdmin()
{
$model =new Account('search');
$userId = Login::model()->getUserId();
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Account']))
{
$model->attributes=$_GET['Account'];
}
$this->render('admin',array(
'model'=>$model,
));
}
UserId is the the id that I would like to restrict that data to. Something along the lines of
$account = Account::model()->findAll(array("condition"=>"user_id = $userId"));
I'm not sure how exactly to go through with this. I've looked around and I know that there are "Criteria" that I could update in the model and there is also a "Filter function in the view". Should I be using one of these two to limit the accounts shown? Or can I do something directly from the controller?
Here is the code in the view
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'account-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'user_id',
'name',
'mobile_comp',
'msisdn',
'pin',
'balance',
/*
'company',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
And the model
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('mobile_comp',$this->mobile_comp,true);
$criteria->compare('msisdn',$this->msisdn);
$criteria->compare('pin',$this->pin);
$criteria->compare('company',$this->company,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Change your actionAdmin to the following
public function actionAdmin()
{
$model =new Account('search');
$userId = Login::model()->getUserId();
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Account']))
{
$model->attributes=$_GET['Account'];
}
$model->user_id = $userId;
$this->render('admin',array(
'model'=>$model,
));
}
The above code will load only the current user record.
Just Solved - Different way than Think Different:
In my model I added a condition
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$userId = Login::model()->getUserId();
$criteria->addCondition(array("condtion"=>"user_id = $userId"));
$criteria->compare('name',$this->name,true);
$criteria->compare('mobile_comp',$this->mobile_comp,true);
$criteria->compare('msisdn',$this->msisdn);
$criteria->compare('pin',$this->pin);
$criteria->compare('company',$this->company,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

search function in yii gridview

I have project implemented in Yii. Grid view search function is not working in server. In localhost Grid view search function is working. What could be issue in that. please suggest me where should i change.
my Recipe controller:
public function actionAdmin()
{
$model=new Recipe('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Recipe']))
$model->attributes=$_GET['Recipe'];
$this->render('admin',array(
'model'=>$model,
));
}
My model part:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$pagination=array('pageSize'=>'10');
$criteria->compare('recipe_id',$this->recipe_id);
$criteria->compare('posted_id',$this->posted_id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('type',$this->type);
$criteria->compare('cuisinename',$this->cuisinename);
$criteria->compare('course_id',$this->course_id);
$criteria->compare('details',$this->details,true);
$criteria->compare('serving_size',$this->serving_size,true);
$criteria->compare('calorie_count',$this->calorie_count);
$criteria->compare('preparation_time',$this->preparation_time);
$criteria->compare('cooking_instructions',$this->cooking_instructions,true);
$criteria->compare('garnishing_instructions',$this->garnishing_instructions,true);
$criteria->compare('serving_instructions',$this->serving_instructions,true);
$criteria->compare('recipe_image',$this->recipe_image,true);
$criteria->compare('recipe_small_image',$this->recipe_small_image,true);
$criteria->compare('status',$this->status,true);
$criteria->compare('posting_time',$this->posting_time,true);
$criteria->compare('pLike',$this->pLike);
$criteria->compare('pDislike',$this->pDislike);
$criteria->compare('images',$this->images,true);
$criteria->compare('Ingredient_TypeId',$this->Ingredient_TypeId,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,'pagination'=>$pagination
));
}
my view part:
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'mygridview',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
//'id',
'name',
array(
'class'=>'CButtonColumn','template'=>'{update}{delete}',
),
),
)); ?>
Handle your javascript following way, there will be no double jquery problem after this, add all other js in the 'js' array
$baseurl = Yii::app()->baseUrl;
$cs = Yii::app()->clientScript;
$cs->scriptMap = array('jquery.min.js'=>$baseurl.'/js/jquery-2.0.3.min.js', 'jquery.js'=>$baseurl.'/js/jquery-2.0.3.min.js');
$cs->registerCoreScript('jquery');
Yii::app()->clientScript->addPackage('other-required-scripts', array(
'baseUrl'=>$baseurl,
'js'=>array(
"js/abc.js",
"js/bcd.js",
//...
),
'depends'=>array('jquery')
));
Yii::app()->clientScript->registerPackage('other-required-scripts');

YII model ::: CGridView Search by specific attribute using criteria

I am new in yii. I want to search by attribute (field name) from my model and need to view in view page or another page by zii.widgets.grid.CGridView.
how can I create a search() function in model by findByAttribute()
so that we can show the results by attribute without any search
here is my model function but it is not working.. :: ERROR Undefined variable: pp_requisitionno
public function searchView()
{
$criteria=new CDbCriteria();
$criteria->select= 'pp_requisitionno';
$criteria->addSearchCondition('pp_requisitionno',$pp_requisitionno);
$criteria->compare('pp_requisitionno',$this->pp_requisitionno);
$criteria->condition = 'pp_requisitionno=:pp_requisitionno';
$criteria->params = array(':pp_requisitionno'=>Yii::app()->Request->Getpost('pp_requisitionno'));
$model = Requisitiondt::model()->find();
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Any help please...
It's probably going to be more useful to define a general search function that can be reused for different searches. This can be done in the following way:
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
$criteria = new CDbCriteria;
//Define all your searchable fields here
$criteria->compare('t.title', $this->title, true);
$criteria->compare('t.type', $this->type, true);
$criteria->compare('t.published', $this->published, true);
$criteria->compare('category.id', $this->category_id, true);
//Add any other criteria, like the default sort order etc.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Then in your controller you can use the search like this;
pubic function actionSearch(){
$model = new Requisitiondt;
if (isset($_POST)){
$model->attributes = $_POST;
$dataProvider = $model->search();
$this->render('searchView', array('dataProvider' => $dataProvider));
}
}
The view 'searchView' then looks like this;
<?php
$this->widget('CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
//Add in whatever columns you want the grid to show
)
));
?>
Obviously you'll need to replace your own model names and field names, but this is the general idea. This way will search for any attributes you include in the POST request,and is more reusable.
You need use Relations in your model, read here.
Then add 'filter' => $model, in your GridView Widget options array

Yii CActiveDataProvider MANY::MANY

i got 2 models: Project and Users connected with (User.php):
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
"projects"=>array(self::MANY_MANY, 'Project','projects_users(user_id, project_id)'),
);
}
I want to show all users in CActiveDataProvider who are not connected with project. How can i do it?
I found solution:
$criteria=new CDbCriteria;
foreach($model->users as $cur) {
$criteria->addCondition("ID != ".$cur->ID);
}
$users=User::model()->findAll($criteria);
$dataProvider2=new CActiveDataProvider('User');
$dataProvider2->data = $users;
Try this:
$users = User::model()->with('projects')->findAll(array(
'together' => true,
'condition' => 'projects.id IS NULL',
));

Yii CGridview not filtering

I'm trying to work with a YII CGridview to display some data.
This is home my model search function looks like:
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ip',$this->ip,true);
$criteria->compare('first_use',$this->first_use,true);
$criteria->compare('last_use',$this->last_use);
$criteria->compare('memberid',$this->memberid);
$criteria->compare('countryid',$this->countryid);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
And this is how my view looks like
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'iplog-grid',
'dataProvider'=>$oIPLog->search(),
'filter'=>$oIPLog,
'summaryText' => 'showing you {start} - {end} of {count} logged Ips',
'columns'=>array(
array(
'name'=>'ip',
'type'=>'raw',
),
array(
'name'=>'first_use',
'type'=>'datetime',
),
array(
'name'=>'last_use',
'type'=>'datetime',
),
),
));
Displaying the CGridview works, but I can't seem to get the filter on top of it to work. It sends the call and I don't get any error as reponse, it just returns the whole unfiltered data again..
Any clues?
And how exactly does your controller look like?
For the CGridview filter to work you need to check in your controller if there are any filters set and then return the filtered object.
To clarify, something like this should be placed into your controller action
$oObject = new Object('search');
if (isset($_GET['Object'])) {
$oObject->attributes = $_GET['Object'];
}
Hope this helps
You Have to apply these points:
1.Specify the global variable($_REQUEST) in function of your controller
for example
$model = new User('search');
$model->unsetAttributes(); // clear any default values
if (isset($_REQUEST['User'])){
$model->attributes = $_REQUEST['User'];
}
$this->render('admin', array(
'model' => $model,
));
Set method type in search form
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl('user/admin'),
'method'=>'POST',
)); ?>
3.In Cgrid view you have to define the url like
'ajaxUrl'=>Yii::app()->createUrl( 'controller/function' ),

Categories