我在高级搜索表单(已批准,审核日期)中有两个字段,我为其创建了radiobuttons。
<div class="row">
<?php echo $form->labelEx($model,''); ?>
<?php
echo $form->radioButtonList($model,'ReviewedDate',
array('1' => 'Reviewed', '' => 'Not Reviewed'),
array(
'template'=>'{input}{label}',
'separator'=>'',
'labelOptions'=>array(
'style'=> '
padding-left:13px;
width: 60px;
float: left;
'),
'onclick' => 'this.form.submit()',
'style'=>'float:left;',
)
);
?>
<div style="clear: both;"></div>
<?php echo $form->error($model,'ReviewedDate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,''); ?>
<?php
echo $form->radioButtonList($model,'Approved',
array('0' => 'Rejected', '1' => 'Approved'),
array(
'template'=>'{input}{label}',
'separator'=>'',
'labelOptions'=>array(
'style'=> '
padding-left:13px;
width: 60px;
float: left;
'),
'style'=>'float:left;',
'onclick' => 'this.form.submit()',
'uncheck' => NULL,
)
);
?>
<div style="clear: both;"></div>
<?php echo $form->error($model,'Approved'); ?>
</div>
以下是我为模型中的两个字段创建的条件。
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with=array('apartment');
$criteria->compare('t.Id',$this->Id);
$criteria->compare('ApartmentId',$this->ApartmentId);
$criteria->compare('Approved',$this->Approved);
$criteria->compare('CreatedDate',$this->CreatedDate,true);
$criteria->compare('EmailBody',$this->EmailBody,true);
$criteria->compare('MailSubject',$this->MailSubject,true);
$criteria->compare('MessageType',1);
$criteria->compare('Name',$this->Name,true);
// $criteria->compare('ReviewedDate',$this->ReviewedDate);
$criteria->compare('SmsText',$this->SmsText,true);
$criteria->compare('UpdatedDate',$this->UpdatedDate,true);
$criteria->compare('PostedByUserFlatId',$this->PostedByUserFlatId);
$criteria->compare('Comments',$this->Comments,true);
$criteria->compare('ModifiedBy',$this->ModifiedBy,true);
if(isset($this->ReviewedDate) )
{
$criteria->addCondition('ReviewedDate IS NOT NULL');
$criteria->addCondition('UpdatedDate > CreatedDate');
$criteria->compare('ReviewedDate','0000-00-00 00:00:00');
}
else
{
$criteria->addCondition('updateddate = createddate');
$criteria->addCondition('ReviewedDate IS NULL');
}
if ($this->Approved)
{
$criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 1 ';
}
else
{
$criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 0 ';
}
$criteria->compare('apartment.Name',$this->groupz_search, true);
$criteria->compare('apartment.SocietyCode',$this->groupz_code_search, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
现在,当我尝试单击已审核/未审核的按钮时,将单击被拒绝时应执行的查询。
SQL query to be executed - SELECT count(*) FROM `message_template` `t` LEFT OUTER JOIN `apartment` `apartment` ON (`t`.`ApartmentId`=`apartment`.`Id`) WHERE (((MessageType=1) AND (updateddate = createddate)) AND (ReviewedDate IS NULL);
SQL executed - SELECT * FROM `message_template` `t` LEFT OUTER JOIN `apartment` `apartment` ON (`t`.`ApartmentId`=`apartment`.`Id`) WHERE (((MessageType=1) AND (updateddate > createddate)) AND (ReviewedDate IS NULL) and (Approved=0));
为什么会发生这种情况,我该如何调试?
修改 * 控制器 *
<?php
class MessageTemplateController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
# 'users'=>array('admin'),
'users'=>array('@'),//vishnu
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
// $model = MessageTemplate::model()->findByPk($id);
$model = $this->loadModel($id);
$this->render('view', array(
'model' => $model,
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new MessageTemplate;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['MessageTemplate']))
{
$model->attributes=$_POST['MessageTemplate'];
if($model->save())
$this->redirect(array('view','id'=>$model->Id));
}
$this->render('create',array(
'model'=>$model,
)
);
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model_mt=new Messagesintable;
$model_al=new AuditLogin;
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['MessageTemplate']) && isset($_POST['AuditLogin']))
{
$model->attributes=$_POST['MessageTemplate'];
list($name,$mobile,$email)=retrieve_persondetails($id);
if($model->save())
{
$model_al->attributes=$_POST['AuditLogin'];
if($model->Approved==1)
{
if($name!=NULL)
{
$model_mt->Address= address_xml($name, $mobile, $email);
$message=$model->SmsText;
$approved="-approved";
$model_mt->Message= message_xml($message,$approved);
$model_mt->MsgType=1;
$model_mt->Date=date('Y-m-d');
$model_mt->Time=date('H:i:s');
$model_mt->CustomData='0,0';
$model_mt->save();
}
$model_al->activity="Message Template Approved";
$model_al->moduleId=1003;
$model_al->content=$model->SmsText;
}
else
{
if($name!=NULL)
{
$model_mt->Address= address_xml($name, $mobile, $email);
$message=$model->SmsText;
$approved="-rejected";
$model_mt->Message= message_xml($message,$approved);
$model_mt->MsgType=1;
$model_mt->Date=date('Y-m-d');
$model_mt->Time=date('H:i:s');
$model_mt->CustomData='0,0';
$model_mt->save();
}
$model_al->activity="Message Template Rejected";
$model_al->moduleId=1003;
$model_al->content=$model->SmsText;
}
if ($model_al->save())
$this->redirect(array('admin','id'=>$model->Id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('MessageTemplate');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
// Oh yeah the filter. I just copied your code.
/*
*/
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new MessageTemplate('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['MessageTemplate']))
$model->attributes=$_GET['MessageTemplate'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer $id the ID of the model to be loaded
* @return MessageTemplate the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model=MessageTemplate::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param MessageTemplate $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='message-template-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
function retrieve_persondetails($id)
{
$hostname="localhost";
$username=Yii::app()->db->username;
$password=Yii::app()->db->password;
$database="apartment";
$connection = mysql_connect($hostname,$username,$password);
mysql_select_db($database);
$sql = "SELECT p.Name,p.Mobile,p.Email from person p,flat f,userflatmapping u,message_template m, society_profile s where m.id=$id and u.id=m.postedbyuserflatid and f.id=u.flatid and p.id=f.registeredpersonid and u.dontsendsmstome=0 and s.apartmentId=f.apartmentid and s.sms_enabled=1;";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$name=$row['Name'];
$mobile=$row['Mobile'];
$email=$row['Email'];
}
if(isset($name))
return array($name,$mobile,$email);
mysql_close($connection);
}
function address_xml($name,$mobile,$email)
{
$address_xml = '<tolist><to><name>' . $name . '</name><contactpersonname>' . $name . '</contactpersonname><number>' . $mobile . '</number><email>' . $email . '</email><prefix></prefix><contactpersonprefix></contactpersonprefix></to></tolist><from><name>' . $name . '</name><number>' . $mobile . '</number></from>';
return $address_xml;
}
function message_xml($message,$approved)
{
$message_xml ='<shorttext>' . $message.''.$approved. '</shorttext>';
return $message_xml;
}
修改规则
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('ApartmentId, Approved, MessageType, PostedByUserFlatId', 'numerical', 'integerOnly'=>true),
array('Name, SmsText, Comments, ModifiedBy', 'length', 'max'=>255),
array('CreatedDate, EmailBody, MailSubject, ReviewedDate, UpdatedDate', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('Id, ApartmentId, Approved, CreatedDate, EmailBody, MailSubject, MessageType, Name, ReviewedDate, SmsText, UpdatedDate, PostedByUserFlatId, Comments, ModifiedBy,apartment.Name,apartment.SocietyCode,groupz_search,groupz_code_search', 'safe', 'on'=>'search'),
//array('ReviewedDate','default',
//'value'=>new CDbExpression('NOW()'),
//'setOnEmpty'=>false,'on'=>'update'),
);
}
答案 0 :(得分:0)
您在致电$criteria->condition = '';
$criteria->addCondition();
使用$criteria->condition = '';
为CDbCriteria对象设置一个全新的条件,这意味着您之前的所有$criteria->addCondition();
都将丢失。
所以改变这个,例如:
$criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 0 ';
对此:
$criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->addCondition("(updateddate > createddate) AND (revieweddate IS NOT NULL)");
答案 1 :(得分:0)
这段代码几乎是正确的:
if ($this->Approved)
{
$criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 1 ';
}
else
{
$criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 0 ';
}
正如“davey”所说,你必须“清理”你的搜索功能,例如你可能会改变你的代码:
if ($this->Approved)
{
//this line is unnecessary $criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->addCondition(' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 1 ');
}
else
{
//this line is unnecessary $criteria->addCondition("approved='" . $this->Approved . "'");
$criteria->addCondition(' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 0 ');
//if you dont want approved = 0 then you must change above line with:
$criteria->addCondition(' (updateddate > createddate) AND (revieweddate IS NOT NULL) ');
}
答案 2 :(得分:0)
调试:
首先你确定你通过GET发送表格参数吗?
<form action="/controller/action" method="get">
第二次测试你的价值观我会做这样的事情:
if(isset($this->Approved)){
if($this->Approved==0){
...
}elseif($this->Approved==1){
...
}else{
throw new CHttpExeption(404,'unknown approval');
}
}
所以,如果没有提交任何内容 - &gt;不会添加任何标准
希望这有帮助
祝你好运