Controller
<?php
class TasksController extends Controller
{
public function actionIndex()
{
$model=new Tasks;
$this->render('index',array(
'model'=>$model
));
}
public function actionCreate()
{
$model=new Tasks;
if(isset($_POST['Tasks']))
{
$model->attributes=$_POST['Tasks'];
if($model->save())
$this->redirect(array('view','id'=>$model->task_id));
}
$this->render('index',array(
'model'=>$model,
));
}
public function actionView($id){
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
public function actionFinish(){
if(isset($_POST['Tasks']['task_id'])) {
$id=$_POST['Tasks']['task_id'];
$model=$this->loadModel($id);
$model->complete=1;
$model->end_date = new CDbExpression('NOW()');
if($model->save())
$this->redirect(array('view','id'=>$model->task_id));
}
}
public function loadModel($id)
{
$model=Tasks::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
// Uncomment the following methods and override them if needed
/*
public function filters()
{
// return the filter configuration for this controller, e.g.:
return array(
'inlineFilterName',
array(
'class'=>'path.to.FilterClass',
'propertyName'=>'propertyValue',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
*/
}
View
<?php
/* @var $this TasksController */
$this->breadcrumbs=array(
'Tasks',
);
$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
echo CHtml::errorSummary($model);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter' => $model,
'columns'=>array(
array(
'name' => 'client_search',
'filter' => CHtml::listData(Client::model()->findAll(), 'client_id', 'name'),
'value'=>function($data) {
return $data->client->name;
}
),
'task'
)
));
echo CHtml::submitButton('Login');
$this->endWidget('CActiveForm');
Model
<?php
/**
* This is the model class for table "tasks".
*
* The followings are the available columns in table 'tasks':
* @property integer $task_id
* @property integer $client_id
* @property string $task
* @property string $start_date
* @property string $end_date
* @property integer $complete
*
* The followings are the available model relations:
* @property Client $client
*/
class Tasks extends CActiveRecord
{
public $client_search;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Tasks the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tasks';
}
public function finish(){
echo 'www';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('client_id, task', 'required'),
array('client_id, complete', 'numerical', 'integerOnly'=>true),
array('task', 'length', 'max'=>5000),
array('start_date','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'insert'),
array('task_id, client_search, task, start_date, end_date, complete,client_id', 'safe', 'on'=>'search'),
);
}
public function getClient_Search() {
return $this->client_search;
}
/**
* @return array relational rules.
*/
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(
'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'task_id' => 'Task',
'client_id' => 'Client',
'task' => 'Task',
'start_date' => 'Start Date',
'end_date' => 'End Date',
'complete' => 'Complete',
'client_search' => 'Клиент'
);
}
/**
* 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()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('client');
$criteria->compare('t.task_id',$this->task_id);
#$criteria->compare('client_id',$this->client_id);
$criteria->compare('t.client_id', $this->client_search);
$criteria->compare('t.name',$this->client->name);
$criteria->compare('t.task',$this->task,true);
$criteria->compare('t.start_date',$this->start_date,true);
$criteria->compare('t.end_date',$this->end_date,true);
$criteria->compare('t.complete',$this->complete);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'client_search'=>array(
'asc'=>'client.client_id',
'desc'=>'client.client_id DESC',
),
'*',
),
),
));
}
}