I have a custom search in my model noted below, It works with out any issue in MySQL and when I step through the code with NetBeans Yii builds the query properly but returns no rows. I run the same query in MySQL and I get the intended rows of information. Is there something I am doing wrong?
public function fnSearch(){
$nParts=explode(" ", $_REQUEST['fnText']);
$criteria=new CDbCriteria;
$criteria->condition='first_name like ":fname" and last_name like ":lname"';
$criteria->params=array(';fname'=>trim($nParts[0]).'%', ':lname'=>trim($nParts[1]).'%');
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
My test form looks like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'test-form',
'enableAjaxValidation'=>false,
)); ?>
<?= CHtml::textField('fnText'); ?>
<?php $this->endWidget(); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employees-grid',
'dataProvider'=>$model->fnSearch(),
// 'filter'=>$model,
'columns'=>array(
'first_name',
'last_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
The controller has this:
public function actionTest(){
$model=new Employees;
$this->render('test',array(
'model'=>$model,
));
}
Related
i made a simple form with checkboxlist() when i submit the form using get it goes to the next page, but the url is nasty looking.
http://mysite/dev/research/abc/all/?Abc[type]=N&Abc[type][]=1&Abc[type][]=2&Abc[type][]=3&yt0=Search
how would i change it to something like this?
http://mysite/dev/research/abc/all/?type=N&type[]=1&type[]=2&type[]=3
or if possible something nicer looking like
?type=1,2,3
note that i don't want &yt0=Search either
in my controller
public function actionIndex()
{
$model=new ABC;
$this->render('index',array(
'model'=>$model
));
}
in my view i have this
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'shop',
'action'=>$this->createUrl('/modulename/abc/all'),
'method'=>'get',
'enableAjaxValidation'=>false,
));
echo $form->checkBoxList($model,'type', $arr,
array('separator'=>'',
'template'=>'<div class="col-md-6 col-xs-12">{input} {label}</div>',
'uncheckValue'=>N,
)
);
echo CHtml::submitButton('Search', array('class'=>'btn btn-success'));
echo CHtml::button('Clear Filter', array('class'=>'btn btn-link', 'type'=>'reset'));
$this->endWidget();
?>
UPDATE:
figured it out. had to add 'name'=> 'type'
but how do i remove &yt0=Search
echo $form->checkBoxList($model,'type', $arr,
array('name'=> 'type',
'separator'=>'',
'template'=>'<div class="col-md-6 col-xs-12">{input} {label}</div>',
'uncheckValue'=>N,
you could use CHtml:button to not post the &yt0=Search
<?php
echo CHtml::button('Search',
array(
'submit'=>array('/modulename/abc/all'),
'class'=>'btn btn-success'
)
);
?>
is there a parameter that I can use with ClistView to get the commments of a given post_id
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comment',
'template'=>"{items}\n{pager}",
)); ?>
I'd like to add post_id = value in order to only list the comment with the given post_id
I put a search function in my comment model
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('post_id',$this->post_id,true);
return new CActiveDataProvider('Comment', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'status, create_time DESC',
),
));
}
Thank in advance for your help. I cannot find in the documentation.
My Goal:
I am trying to filter a model, based on a GET parameter, and populate a form based on this.
What I require is that when users select the update icon on the Gridview
I will
grab the 'telephone number' they want to edit,
populate a form with the telephonenumber's data
allow the user to edit this data for the telephonenumber and submit
I then run my own custom sql update query based on the new data.
My Problem
My gridview can successfully capture the telephonenumber for a selected row.
It can successfully send this to the Controller Update method (see below)
I cannot however filter a model based on this telephonenumber and then populate a form with this model.
My Error
get_class() expects parameter 1 to be object, array given
/framework/web/helpers/CHtml.php(2220)
/framework/web/helpers/CHtml.php(2220): get_class(array())
/framework/web/helpers/CHtml.php(1236): CHtml::resolveName(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/widgets/CActiveForm.php(562): CHtml::activeLabelEx(array(), "TelephoneNumbers_TelephoneNumber", array())
/views/dateAudiid/editupdateform.php(18): CActiveForm->labelEx(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/CBaseController.php(126): require("/var/www/OMReport/protected/views/dateAudiid/editupdateform.php")
Here's my Gridview.
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}',
'buttons'=>array
(
'update' => array
(
'url'=>'Yii::app()->createUrl("dateAudiid/Update/",array("TelephoneNumbers_TelephoneNumber"=>$data->TelephoneNumbers_TelephoneNumber))',
),
),
),
'TelephoneNumbers_TelephoneNumber',
'FormId_Formid',
'Date',
'AudibeneID_Audibene_ID'
),
));
Here's my Controller
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$criteria=new CDbCriteria;
$criteria->compare('TelephoneNumbers_TelephoneNumber',$_GET['TelephoneNumbers_TelephoneNumber'],true);
$criteria->limit = 1;
$criteria->offset = 1;
$model = DateAudiidCondensedByAudibeneId::model()->findAll($criteria);
$this->render('editupdate',array('model'=>$model));
}
Here's my View
<?php
/* #var $this DateAudiidController */
/* #var $model DateAudiidCondensedByAudibeneId */
?>
<h1>Update Assignments </h1>
<?php echo $this->renderPartial('editupdateform', array('model'=>$model)); ?>
Here's my Form
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'date-audiid-condensed-by-audibene-id-customupdate-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php
echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'TelephoneNumbers_TelephoneNumber'); ?>
<?php echo $form->textField($model,'TelephoneNumbers_TelephoneNumber'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'FormId_Formid'); ?>
<?php echo $form->textField($model,'FormId_Formid'); ?>
<?php echo $form->error($model,'FormId_Formid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Date'); ?>
<?php echo $form->textField($model,'Date'); ?>
<?php echo $form->error($model,'Date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->textField($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->error($model,'AudibeneID_Audibene_ID'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Thoughts / What I've tried so far
I am returning the model as an array...but my form wants it as an object.
I need to change something in the way I filter my model in the Controller Update action, however I cannot see how to do this.
All my other methods use similar model filtering code.
here is what you can do to fix this :
your form is trying to use models labeling but you have given an array, ->findAll() will return an array of models, here you need a single object of model
the error your having is because $model in filter of your grid needs to be an object of model which here will try to validate using this model, so won't work when you are giving it an array of your models,
if you want to filter the results based on something, you need to do this where the dataprovider of your grid is being populated, so in this case, is in $model->search()
get the parameter and append it to that criteria
so your grid could look like this:
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider' => $model->search(), //create a new model with search scenario
'filter' => $model, // here use that model to validate fields
'columns'=>array(
.
.
.
),
));
and in your models search method:
public function search() {
$criteria = new CDbCriteria;
// grab the sent data and use it here
$tel = Yii::app()->request->getParam('TelephoneNumbers_TelephoneNumber' , null);
if(!empty($tel))
$criteria->compare('TelephoneNumbers_TelephoneNumber' , $tel , true);
.
.
.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Here's what eventually worked for me
The usage of 'findByAttributes' to filter and return a model to the Form
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$model = DateAudiidCondensedByAudibeneId::model()->findByAttributes(array("TelephoneNumbers_TelephoneNumber" => $_GET['TelephoneNumbers_TelephoneNumber']));
$this->render('editupdate',array('model'=>$model));
}
I am a newbei in yii, when a person clicks on a category display him all products under that particular category in a gridview
view
productcategory
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'admin-grid',
'dataProvider'=>$model->SelectedCategoryProducts,
'filter'=>$model,
'columns'=>array(
'Name',
'Model',
'Brand',
'Price',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
controller
product
public function actionProductcategory($id)
{
$model= Product::model()->SelectedCategoryProducts($id);
var_dump($model);
$this->render('productcategory',array(
'model'=>$model,'id'=>$id,
));
}
model
product
public function SelectedCategoryProducts($id)
{
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
var_dump($dataProvider);
return $dataProvider;
}
CException
Property "CActiveDataProvider.sellerSelectedCategoryProducts" is not defined.
PLEASE HELP! I am losing my mind on this ... not able to display in gridview.
Pass $id to your view file.
$this->render('productcategory',array('model'=>$model,'id'=>$id));
Then pass id to model function in ccgridview function.
'dataProvider'=>$model->SelectedCategoryProducts($id),
Hope this might help
Controller file
public function actionProductcategory($id)
{
$model=new Product;
$this->render('productcategory',array('model'=>$model,
'id'=>$id));
}
In View file
'dataProvider'=>$model->SelectedCategoryProducts($id),
UPDATE 1
'columns'=>array(
'name',
'model',
'brand',
'price',
change them to lowercase which are your original column names
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
this can retrieve needed data.... first check that data is perfect ... is it right after that take second step....
I am slowly advancing yii
Not how to apply this:
I have a CGridView which shows me data from 3 related tables
I do it this way (modify the default generated by the crud:
model tabla1
public function search()
{
...
...
...
$criteria=new CDbCriteria;
$criteria->with = array('tabla2','tabla3');
$criteria->together = true;
$criteria->addInCondition('t.idtb1',1,3,5,6,7);
$criteria->compare('idtb2',$this->idtb2);
$criteria->compare('date',$this->date,true);
$criteria->compare('tabla2.codigo',$this->codigo,true);
$criteria->compare('tabla3.description',$this->descrip,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The view
...
...
...
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'redeemed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'codigo',
'value'=>'$data->tabla2->codigo',
),
array(
'name'=>'description',
'value'=>'$data->tabla3->descrip',
),
This shows me everything works perfect.
The problem is that I want to use two more tables that are not related, they are tabla4 and tabla5 and aggregate data from both tables to Cgridview.
as can be done for linking data to table1 to be unrelated to tabla4 and tabla5?
Greetings and thanks