Hi All,
I have a tbl_scaffold with columns status_id and inspection_date that I want to be updated from InspectionController actionCreate.
The reason I want the 2 columns of tbl_scaffold to be updated as soon as I create a new inspection is because I don’t want to change it manually since the next inspection due (inspection_date) must be changed after a new inspection was created and the status (status_id) must be the same with the result of last inspection.
/**
* @var private property containing the associated Scaffold model instance.
*/
private $_scaffold = null;
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Inspection;
$model->scaffold_id = $this->_scaffold->id;
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if(isset($_POST['Inspection']))
{
$model->attributes=$_POST['Inspection'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
The tbl_inspection has also column status_id but inspection_date is declared as public on Inspection model
class Inspection extends ScaffRegActiveRecord
{
public $inspection_date;
and included on Inspection view/_form as date picker
<?php
/* @var $this InspectionController */
/* @var $model Inspection */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'inspection-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,'status_id'); ?>
<?php echo $form->dropDownList($model,'status_id', $model->getStatusOptions(), array('prompt'=>'Select Status')); ?>
<?php echo $form->error($model,'status_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'inspection_date'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute' => 'inspection_date',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'showAnim' => 'slide',
'appendText' => '',
),
));
?>
<?php echo $form->error($model,'inspection_date'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
The inspection is under ScaffoldContext
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'scaffoldContext + create index admin', //check to ensure valid project context
//'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* In-class defined filter method, configured for use in the above filters() method
* It is called before the actionCreate() action method is run in order to ensure a proper scaffold context
*/
public function filterScaffoldContext($filterChain)
{
//set the scaffold identifier based on either the GET input
//request variables
if(isset($_GET['pid']))
$this->loadScaffold($_GET['pid']);
else
throw new CHttpException(403,'Must specify a scaffold before performing this action.');
//complete the running of other filters and execute the requested action
$filterChain->run();
}
and can only create an inspection while a valid Scaffold context is open in view.php (CDetailView)
<?php
/* @var $this ScaffoldController */
/* @var $model Scaffold */
$this->breadcrumbs=array(
'Scaffolds'=>array('index'),
$model->id,
);
$this->menu=array(
array('label'=>'List Scaffold', 'url'=>array('index')),
array('label'=>'Create Scaffold', 'url'=>array('create')),
array('label'=>'Update Scaffold', 'url'=>array('update', 'id'=>$model->id)),
array('label'=>'Delete Scaffold', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage Scaffold', 'url'=>array('admin')),
array('label'=>'Create Inspection', 'url'=>array('inspection/create', 'pid'=>$model->id)),
);
?>
<h1>View Scaffold #<?php echo $model->id; ?></h1>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
//'id',
array(
'name' => 'status_id',
'value' => CHtml::encode($model->getStatusText())
),
'inspection_date',
),
)); ?>
<br />
<h1>Scaffold Inspections</h1>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$inspectionDataProvider,
'itemView'=>'/inspection/_view',
)); ?>
Note:
I removed the other columns of tbl_scaffold and tbl_inspection to shortened the codes.
I’m sure this is possible with Yii but I don’t know how to do it, please help me.