following is my query how do i place what code in view ,model,controller in yii
select productid, count(*)
from fc_member
group by productid
order by count(*)desc
LIMIT 3
Please let me know i am a newbie
i did try but then do not the how and what of it :(
controller
public function actiondsplayproduct()
{
$model=new Member;
$dataProvider=new CActiveDataProvider('$model',
array(
'criteria'=>array(
'select'=>'productid, COUNT( * )as Cproductid',
'from'=>'fc_member',
'group'=>'productid',
'order'=>'COUNT( * ) ',
),
)
);
$this->render('dsplayproduct',array(
'dataProvider'=>$dataProvider,
));
//$this->render('dsplayproduct',array('model'=>$model));
}
view
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=> $dataProvider,
'filter'=>$model,
'columns'=>array(
'productid',
'Cproductid',
)
));
And also get the below error
PHP warning
include($model.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory
Please let me know I am a newbie
I did try but then do not know the how and what of it :(
following r d changes
model
`public function top_selling_products() {
$criteria = new CDbCriteria;
$criteria->select = 'productid , count(*) as pid_count';
$criteria->from='fc_member';
$criteria->group = 'productid';
$criteria->order = 'pid_count desc';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}`
controoler
`
public function actiondsplayproduct()
{
$dataProvider=new CActiveDataProvider('Member');
$this->render('dsplayproduct',array(
'dataProvider'=>$dataProvider,
));
}
view
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>Member::model()->top_selling_products(),
'enablePagination' => false,
'columns'=>array(
'pid',
'pid_count',
array(
'class'=>'CButtonColumn',
),
),
)); `
but i get the folowing error
Property "CDbCriteria.from" is not defined.
Plz let me now where am i wrong
I think it's not string $model than you want to put into CActiveDataProvider class ...
So, it's not
CActiveDataProvider('$model', ...
But
CActiveDataProvider('model', ...
Or
CActiveDataProvider($model, ...
error says that you dont have "$model.php" file - so you have to put there valid and existing name for model file.
for example:
new CActiveDataProvider('model', ...
Related
I am traying to make a dropdown pagination in my project, but it's not working correctly.
Whatever the number that I select in the dropdownlist, it still shows 10 items in the page.
What I am missing ?
Thank you.
Screenshot
The view code :
<?php
//$dataProvider->pagination->pageSize = 25;
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
echo CHtml::dropDownList('pageSize',$pageSize,array(5=>5,10=>10,15=>15,20=>20,25=>25,30=>30),
array('onchange'=>"$.fn.yiiGridView.update('packages-grid',{ data:{pageSize: $(this).val() }})",
'empty'=>'-- Select Page Range --','style'=>'width:198px;'));
$this->widget('zii.widgets.CListView', array(
'id'=>'propertylistview',
'dataProvider'=>$dataProvider,
'summaryText'=>'',
'itemView'=>'_propertyview',
'sortableAttributes' => array(
.........
)
));
?>
The controller code :
public function actionAdmin(){
if (isset($_GET['pageSize'])) {
Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
unset($_GET['pageSize']);}
$model=new Packages('search');
$model->unsetAttributes();
if(isset($_GET['Packages']))
$model->attributes=$_GET['Packages'];
$dataProvider=new CActiveDataProvider('Packages');
$this->render('admin',array('model'=>$model,'dataProvider'=>$dataProvider,
));
}
I already added this to model search
return new CActiveDataProvider(get_class($this),array(
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
'criteria'=>$criteria,
));
and this to config/main.php
'params'=>array('defaultPageSize'=> '10'),
You're doing an ajax call but in your widget you do not state any update part to update. ajaxUpdate is the solution.
And you are using 'onchange'=>"$.fn.yiiGridView.update while using a CListView so change the js to 'onchange'=>"$.fn.yiiListView.update and:
<?php
// your code
$this->widget('zii.widgets.CListView', array(
'id'=>'propertylistview',
'ajaxUpdate' => 'propertylistview', //add this line
'dataProvider'=>$dataProvider,
'summaryText'=>'',
'itemView'=>'_propertyview',
'sortableAttributes' => array(
//etc.. )
));
?>
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');
I'm trying to output data into the CListView of the current user only. So far, if I put in the $dataProvider, it only outputs ALL the records from the database.
This is my current code:
$current = Yii::app()->user->id;
$currentid = Yii::app()->db->createCommand("select * from content where id = ". $current)->queryRow();
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider, //This is the original. I tried replacing it
//with $currentid but errors.
'itemView'=>'_view2',
'template'=>'{items}<div>{pager}</div>',
'ajaxUpdate'=>false,
));
From what I understand from the Yii Documentations, $dataProvider stores all the data within the database and places it inside the dataProvider itself and my "_view2" uses that to output all the records.
My Controller codes for the showing/view is as follows:
public function actionView()
{
$post=$this->loadModel();
if(Persons::model()->compare_country(explode("|",$post->country)))
{
$post->view_count = $post->view_count + 1;
Yii::app()->db->createCommand("UPDATE content SET view_count = {$post->view_count} WHERE id = {$post->id}")->execute();
//$post->save();
$comment=$this->newComment($post, 'view');
if (!empty(Yii::app()->session['announcement_message']))
{
Yii::app()->user->setFlash('message',Yii::app()->session['announcement_message']);
Yii::app()->session['announcement_message'] = null;
}
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
'view'=>'view',
));
}
else
{
$this->redirect(Yii::app()->createAbsoluteUrl('news/index',array('page'=>'1')));
}
}
public function actionShow($id)
{
$post=$this->loadModel($id);
$comment=$this->newComment($post);
$attachments=Attachments::model()->findAllByAttributes(array(
'content_id' => $id,
));
$this->render('show',array(
'model'=>$post,
'comment'=>$comment,
'attachments'=>$attachments
));
}
If you wanted to see my _view2, these are my codes:
<div class="profile-member-post-box announcement" >
<div class="events-post-bodytext profile-member-info">
<?php $person=Persons::model()->findByAttributes(array('party_id'=>$data->party_id));
if ($person->party_id === Yii::app()->user->id)
{
?>
<span><?=CHtml::link($data->title, array('view', 'id'=>$data->id), array('class' => 'titlelink'));?></span>
<?php
$country=Lookup_codes::model()->findByAttributes(array('id'=>$person->country));
$location = empty($country) ? '' : 'of '.$country->name;
$sysUser=User::model()->findByAttributes(array('party_id'=>$data->party_id));
?>
<p>
By: <?php echo CHtml::link($person->getusername(), array('persons/view/id/'.$person->showViewLinkId())); ?>
<span class="date2"> - <?php echo date('M j, Y',strtotime($data->date_created)); ?></span>
</p>
<div>
<?php if(Yii::app()->partyroles->isAdmin() || ((get_access('Announcement','edit') && (Yii::app()->user->id == $data->party_id)) || (get_local_access('sub-admin','edit',$data->id)))):?>
Edit | <?php endif;?> <?php echo (Yii::app()->partyroles->isAdmin() || (get_access('Announcement','delete') && (Yii::app()->user->id == $data->party_id)) || (get_local_access('sub-admin','delete',$data->id))) ? CHtml::link('Delete','#',array('submit'=>array('delete','id'=>$data["id"]),'confirm'=>'Are you sure you want to delete this item?')) : NULL?>
</div>
<?php
}
else
?>
</div>
I just need to be able to fix the view to show records only by the current user.
UPDATE!!------------
I'm going to add my actionIndex here:
public function actionIndex()
{
if(get_access('Announcement','view') || get_access('Announcement','view_local'))
{
$id = Yii::app()->user->id;
$condition = Persons::model()->get_view_condition('Announcement');
$criteria=new CDbCriteria(array(
'condition'=>'1=1 '.$condition,
'order'=>'date_modified DESC',
'with'=>'commentCount',
));
/*
if(isset($_GET['tag']))
$criteria->addSearchCondition('tags',$_GET['tag']);
*/
$items=SystemParameters::model()->findAllByAttributes(array(
'name' => 'blogs_per_page',
));
$dataProvider=new CActiveDataProvider('Announcement', array(
'pagination'=>array(
'pageSize'=>strip_tags($items[0]->value),
),
'criteria'=>$criteria,
));
/* $dataProvider=new CActiveDataProvider('Announcement', array(
'pagination'=>array(
'pageSize'=>5,
),
'criteria'=>$criteria,
));*/
//$dataProvider=Announcement::model()->findAll();
$attachments=Attachments::model()->findAllByAttributes(array(
'content_id' => $id,
));
if (!empty(Yii::app()->session['announcement_message']))
{
Yii::app()->user->setFlash('message',Yii::app()->session['announcement_message']);
Yii::app()->session['announcement_message'] = null;
}
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
else
{
$this->redirect(Yii::app()->createAbsoluteUrl('news/index',array('page'=>'1')));
}
}
Your question is very hard to follow... but I'll attempt to answer by giving an example of how to use the CDataProvider and CListView to display all of the Announcements owned by the current logged in User. This assumes the Announcement model's table has a user_id field which contains the id of the User who owns or created it.
First, in your indexAction() in your controller:
// get the logged in user's ID
$userId = Yii::app()->user->id;
// now define the dataprovider, which will do the SQL query for you
$dataProvider = new CActiveDataProvider( // declare a new dataprovider
'Announcement', // declare the type of Model you want to query and display
array( // here we build the SQL 'where' clause
'criteria' => array( // this is just building a CDbCriteria object
'condition' => 'user_id=:id', // look for content with the user_id we pass in
'params' => array(':id' => $userId), // pass in (bind) user's id to the query
//'order'=>'date_modified DESC', // add your sort order if you want?
//'with'=>'commentCount', // join in your commentCount table?
)
)
);
$this->render('index',array( // render the Index view
'dataProvider'=>$dataProvider, // pass in the data provider
));
Then in your index.php view:
// create the CListView and pass in the $dataProvider we created above, in the indexAction
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider, // this is the data provider we just created
'itemView'=>'_view2',
'template'=>'{items}<div>{pager}</div>',
'ajaxUpdate'=>false,
));
I'm trying to use my model with a replacement for the search (custom search function):
public function combineCampInputByDate($startDate,$endDate) {
$criteria=new CDbCriteria;
$criteria->select = 'food.*,SUM(customer) AS customer, SUM(money) AS money';
$criteria->join = 'JOIN foodType food ON foodtype = food.foodtype ';
$criteria->condition = "date BETWEEN '$startDate' AND '$endDate'";
$criteria->group = 'foodtype ';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
the result will be the attributes for the model + the other table.
i'm trying to display them in the view but it states no such attribute as .... (this is clear since the model doesnt have an attribiute that came from the other table)
So how do i use the widget below on the model result??
frustrates, but counting on the experts :)
Danny
Edit:
1. Controller -
public function actionIndex()
{
if (isset($_POST['Filter']) && !empty($_POST['Filter']['date']) ) {
$GetDates = new GetDates();
$this->dates = $GetDates->getDatesFromPostWidget($_POST);
$model = new Campaigns;
}
else
$model=NULL;
$this->render('index',array(
'model' => $model, 'dates' => $this->dates,
));
}
Model -
public function combineCampInputByDate($startDate,$endDate) {
$criteria=new CDbCriteria;
$criteria->select = 'food.*,SUM(customer) AS customer, SUM(money) AS money';
$criteria->join = 'JOIN foodType food ON foodtype = food.foodtype ';
$criteria->condition = "date BETWEEN '$startDate' AND '$endDate'";
$criteria->group = 'foodtype ';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bo-campaigns-grid',
'dataProvider'=>$model->combineCampInputByDate($dates['startDate'],$dates['endDate']),
'filter'=>$model,
),
));
}
In your model class create two attributes: public $customer and public $money.
You can have as many custom attributes as you want, just be consistent with the naming. (You can't use the SQL fieldname AS something if you don't have a something model attribute first)
EDIT: You should also tell the CGridView which columns to display, like this
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bo-campaigns-grid',
'dataProvider'=>$model->combineCampInputByDate($dates['startDate'],$dates['endDate']),
'filter'=>$model,
'columns'=>array(
'customer',
'money',
//etc. for more detailed customization, check the links above
),
),
));
}
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' ),