I'm just trying to wrap my head around the yii framework at the moment and I've created a function to delete multiple database records at once. In the view I've got a grid with a reference to each item and a checkbox next to it
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array(
'class'=>''
),
'columns'=>array(
...
array(
'class'=>'CLinkColumn',
'header'=>'Handle',
'labelExpression'=>'$data->handle',
'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))',
),
array(
'class'=>'CCheckBoxColumn',
'header'=>'Select',
'selectableRows'=>'2',
),
),
Then further down the page I want a button (delete selected) which sends an array of all of the items to a delete function. My thoughts were it would be something akin to this:
<a href="
<?php echo Yii::app()->createUrl('.../item/bulkDelete','array("id" => $data->id)')?>"
class="btn">Delete Selected</a>
But I don't understand how to get a reference for each checked item instead of :
"id" => $data->id
as I used to call pass an item to the view function earlier. If anyone can help it will be greatly appreciated.
Edit:
View:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'itemForm',
'action' => array('.../item/bulkDelete'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array(
'class'=>''
),
array(
'class'=>'CLinkColumn',
'header'=>'Handle',
'labelExpression'=>'$data->handle',
'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))',
),
array(
'class'=>'CCheckBoxColumn',
'header'=>'Select',
'selectableRows'=>'2',
),
... //More Columns
), //End of Grid
...
echo CHtml::SubmitButton('Delete Multiple');
$this->endWidget();
Controller:
public function actionBulkDelete()
{
if(isset($_POST['id'])&& !empty($_POST['id']))
{
Yii::app()->user->setFlash('success', 'Delete Items');
$this->redirect(array('.../item/index'));
}
else
{
Yii::app()->user->setFlash('success', 'No Items Selected');
$this->redirect(array('.../item/index'));
}
}
Wrap the grid view inside a form, and do the following changes in your columns array:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'some-grid-form',
'action' => array('myController/myAction'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'selectableRows' => 2,
'columns'=>array(
array(
'id' => 'id',
'class' => 'CCheckBoxColumn',
),
//... rest of your columns
),
array(
'class'=>'CButtonColumn',
),
),
));
echo CHtml::SubmitButton('Multiple Delete');
$this->endWidget(); // end form
And in your Controller's myAction:
public function actionmyAction()
{
if(isset($_POST['id']) && !empty($_POST['id'])) { //you'll get all the ids in an array
//print_r($_POST['id']);
//your delete function here, also add a few validation here to authenticate deletion
$ids = $_POST['id'];
$criteria = new CDbCriteria;
$criteria->addInCondition('id',$ids);
MyModel::model()->deleteAll($criteria);
//.... render with success flash message etc.
}
}
Related
I want to fetch data from my table using CActiveDataProvider in Yii. Everything seems to be working well but when I want to display the data from another related table using relations, I get an error. 'Undefined variable $data'.
here is my admin.php view:
<h1>Manage Teams</h1>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'team-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'team_name',
array(
'name'=>'league_id',
'type'=>'raw',
'value'=>$data->league->league_name,
),
'create_time',
'update_time',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
and here is my actionAdmin method on TeamController.php
public function actionAdmin()
{
$dataProvider=new CActiveDataProvider('Team', array(
'criteria'=>array(
'order'=>'create_time DESC',
),
'pagination'=>array(
'pageSize'=>20,
),
));
$this->render('admin',array(
'dataProvider'=>$dataProvider,
));
}
The relation is, obviously, a team belongs to a league.
You should just put the $data->league->league_name in quotes and then it will recognize the $data variable. It should look like this:
array(
'name'=>'league_id',
'type'=>'raw',
'value'=>'$data->league->league_name',
),
you need to do it like this :
your column should be
array(
'name'=>'league_id',
'value'=>array($this,'league_name'),
),
and you controller shod have a function like this:
public function league_name($data,$row)
{
return $data->league->league_name;
}
Hello i created a separate search form having input box and button.
in my model i want to search products by category wise...
but problem is that when input box is empty and clicking on search buttons it displays all entries from the database table..
controller code is-
class AddController extends Controller
{
public function actionAddsearch()
{
$model_form = new Add("search");
$attributes = Yii::app()->getRequest()->getPost("Add");
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
}
model code--
class Add extends CActiveRecord
{
public function searchAdd()
{
$criteria = new CDbCriteria();
$criteria->compare("addname", $this->category, TRUE, "OR");
$criteria->compare("category", $this->category, TRUE, "OR");
return Add::model()->findAll($criteria);
}
view code- addsearch.php
<div class="search-bar">
<?php
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
"action"=>$this->createUrl("add/addsearch"),
'type'=>'search',
)
);
echo $form->textFieldRow($model,'category');
echo "        ";
$this->widget('bootstrap.widgets.TbButton',array(
'buttonType'=>'submit',
'type'=>'success',
'size'=>'large',
'label'=>'Search For Products ',
));
$this->endWidget();
?>
</div>
searchResults.php
<?php
echo "<h4>Results for Your Search</h4>";
foreach($models as $model):
$this->beginWidget('bootstrap.widgets.TbDetailView',array(
'type'=>'bordered condensed',
'data' => array(
'Shop Name' =>CHtml::link(CHtml::encode($model->addname),array('add/view','id'=> $model->addid)),
'Category' => $model->category
),
'attributes' => array(array('name' => 'Shop Name', 'label' => 'Name of Shop','value'=>CHtml::link(CHtml::encode($model->addname),
array('add/view','id'=>$model->addid)),'type'=>'raw'),
array('name' => 'Category', 'label' => 'Category of Shop'),
),
)
);
echo "<br><hr><br>";
$this->endWidget();
endforeach;
?>
what is wrong in the code??
I want to display no any product when text box is empty..
thanks in advance
change this
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
To
if($attributes)
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
}
1) Is the 'caterory' attribute safe on search scenario ? (in rules)
2) category is an attribute of the table schema ?
In my view I have a link which is calling a function in Controller. The function in controller is making a pdf. It is supposed to make pdf of only $model->id. But I am unable to send the value of $model->id into controller via my link.
View
<?php
echo CHtml::link('Save/Print',array('print'),array('class'=>'btnPrint btn btn-info','target'=>'new'));
?>
<?php
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'father_name',
'cnic',
'customername',
),
));
?>
Controller
public function actionPrint($id) {
ini_set('max_execution_time',360);
ini_set('memory_limit', '128M');
$mPDF1 = Yii::app()->ePdf->mpdf('','A4');
$mPDF1->SetHTMLHeader('<h3 style="text-align: center;">'.mb_strtoupper(str_replace('Hello','',Yii::app()->name),'UTF-8').'</h3>');
// $id=35;
$records = Candidate::model()->findByPk($id);
$html = '';
$html .= $this->renderPartial('view', array('model'=>$records),true);
$mPDF1->WriteHTML($html, false);
$mPDF1->Output();
}
How will I send the value of id?
go like this for singel link
// $model->id is the id you want to send
echo CHtml::link(
'Save/Print',
Yii::app()->createUrl('Save/Print' , array('id' => $model->id)),
array('class'=>'btnPrint btn btn-info','target'=>'_blank'));
if you want it in a grid
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
array(
'name' => 'id',
'value' => 'echo CHtml::link(
"Save/Print",
Yii::app()->createUrl("Save/Print" , array("id" => $data->id)),
array("class"=>"btnPrint btn btn-info","target"=>"_blank"));',
'type' => 'raw',
),
'name',
'father_name',
'cnic',
'customername',
),
));
I want to show 2 tables with a right join, but the code I wrote does not work as expected. Can anyone tell me what I am doing wrong ?
view : admin.php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'punish-grid',
'dataProvider' => $model->searchJoin(),
'type' => 'striped bordered condensed',
'filter' => $model,
'columns' => array(
array(
'header' => 'No',
'type'=>'raw',
'htmlOptions'=>array('style'=>'width: 25px'),
'value'=>'$this->grid->dataProvider->pagination->currentPage
*$this->grid->dataProvider->pagination->pageSize + $row+1',
),
// i want to display p.kode,p.status from table status
'berlaku_punish',
'nilai',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
and my model : BasePunish.php
public function relations() {
return array(
'idStatus' => array(self::BELONGS_TO, 'Status', 'id_status'),
);
}
public function searchJoin() {
$criteria = new CDbCriteria;
$criteria->select = 'p.kode,p.status,t.nilai,t.berlaku_punish';
$criteria->join= 'RIGHT JOIN status p ON (t.id_status=p.id)';
$criteria->condition = 't.id_status IS NULL';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort'=>array(
'defaultOrder'=>'kode ASC',
),
)
);
}
I might really did not understand what you are asking but still if nothing is working for you then you can try this
array(
'header'=>'Products',
'value'=>array($model,'gridCreateUser'),
),
In this, the value will try to find the function gridCreateUser in the class of which $model is the object. In your case i guess $model is the object of the BasePunish.
So in your BasePunish.php create a function gridCreateUser() and then you can return the value which you want to display in your widget.
eg:-
In your BasePunish.php
public function gridCreateUser($data)
{
// you can access the object $data here
// do what ever you want to do
$value='return whatever you want to return';
return $value;
// this $value will be displayed there
}
I have 2 tables/models:
Tmp1
Header
QuestionText
Tmp2
Header
Part
OutOf
I'm trying to display the columns: Header, QuestionText, Part, OutOf
in a single CGRIDVIEW.
In Tmp1 model:
public function relations()
{
return array(
'tmp2s' => array(self::HAS_MANY, 'Tmp2', 'Header'),
);
}
In Tmp2 Model:
public function relations()
{
return array(
'header' => array(self::BELONGS_TO, 'Tmp1', 'Header'),
);
}
Controller:
public function actionReviewAll()
{
$tmp1 = new Tmp1('search');
$tmp1->unsetAttributes();
if(isset($_GET['Tmp1'])){
$model->attributes=$_GET['Tmp1'];
}
$this->render('ReviewAll',array(
'tmp1'=>$tmp1,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
array(
'name' => 'tmp2.OutOf',
'value' => '$data->tmp2[0].OutOf',
'type' => 'raw'
),
),
)); ?>
Error:
Property "Tmp1.tmp2" is not defined.
Any help is greatly appreciated
You have a sintax error:
Tmp1.tmp2 = Tmp1.tmp2s
The relation alias is with 's'. "tmp2s".
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
'tmp2s.Part',
'tmp2s.OutOf',
),
)); ?>
But you are trying to show a 'self::HAS_MANY' relationship, you can't show that on a normal CGridView widget...
Your code probably works (I haven't tried it), but not in cases where your tmp1 model has no tmp2's. You must make sure there is a tmp2 before accessing it:
'value' => 'isset($data->tmp2) ? $data->tmp2[0].OutOf : Null',
You can also pass a function($data,$row) to value to make it look less messy. (Yes, that's important to me.)