在控制器的admin方法中,无法接收参数来指定数据库查询,
public function actionAdmin()
{
$model=new TblOrders('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['TblOrders']))
$model->attributes=$_GET['TblOrders'];
$this->render('admin',array(
'model'=>$model,
));
}
要实现查询,可以在在模型文件中,找到search()方法,然后在$criteria=new CDbCriteria;后面写上获取GET参数,设置criteria->condition.
代码如下:
<?php
/**
* This is the model class for table "tbl_orders".
*
* The followings are the available columns in table 'tbl_orders':
* @property string $orders_id
* @property string $orders_sn
* @property string $members_id
* @property integer $orders_status
* @property integer $shipping_status
* @property string $consignee
* @property string $country
* @property string $province
* @property string $city
* @property string $address
* @property string $tel
* @property string $mobile
* @property string $email
* @property string $shipping_id
* @property string $shipping_name
* @property string $pay_id
* @property string $pay_name
* @property string $goods_amount
* @property string $add_time
* @property string $confirm_time
* @property string $pay_time
* @property string $shipping_time
* @property integer $pay_status
*/
class TblOrders extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_orders';
}
/**
* @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('orders_status, shipping_status, pay_status', 'numerical', 'integerOnly'=>true),
array('orders_sn, consignee, tel, mobile, shipping_name, pay_name', 'length', 'max'=>16),
array('members_id, country, province, city, shipping_id, pay_id, goods_amount', 'length', 'max'=>8),
array('address', 'length', 'max'=>64),
array('email', 'length', 'max'=>32),
array('add_time, confirm_time, pay_time, shipping_time', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('orders_id, orders_sn, members_id, orders_status, shipping_status, consignee, country, province, city, address, tel, mobile, email, shipping_id, shipping_name, pay_id, pay_name, goods_amount, add_time, confirm_time, pay_time, shipping_time, pay_status', 'safe', 'on'=>'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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'orders_id' => '订单ID',
'orders_sn' => '订单编码',
'members_id' => '会员ID',
'orders_status' => '订单状态',
'shipping_status' => '发货状态',
'consignee' => '收货人',
'country' => '国家',
'province' => '省份',
'city' => '城市',
'address' => '地址',
'tel' => '电话',
'mobile' => '手机',
'email' => '邮箱',
'shipping_id' => '配送方式ID',
'shipping_name' => '配送方式',
'pay_id' => '支付方式ID',
'pay_name' => '支付方式',
'goods_amount' => '总价格',
'add_time' => '添加时间',
'confirm_time' => '确认时间',
'pay_time' => '支付时间',
'shipping_time' => '发货时间',
'pay_status' => '支付状态',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
//添加的代码:获取订单状态信息,然后设置条件来获取相应的数据库信息。
if(isset($_GET['orders_status'])){
$temp=$_GET['orders_status'];
switch ($temp){
case 1:
$criteria->condition="orders_status='".$temp."'";
break;
case 2:
$criteria->condition="orders_status='".$temp."'";
break;
case 3:
$criteria->condition="orders_status='".$temp."'";
break;
case 4:
$criteria->condition="orders_status='".$temp."'";
break;
default:
break;
}
}
/**
*按照特定的条件显示数据表的内容
*/
//创建访问控制对象,该类在components组件文件夹下accessCtrl,该类是自定义的by ping
$accessCtrl=new accessCtrl();
//把上面得到的criteria对象传递到shopmanager方法,得到返回结果
if($temp=$accessCtrl->accessForOrders($criteria)){
$criteria=$temp;
}
//END:按照特定的条件显示数据表的内容
$criteria->compare('orders_id',$this->orders_id,true);
$criteria->compare('orders_sn',$this->orders_sn,true);
$criteria->compare('members_id',$this->members_id,true);
$criteria->compare('orders_status',$this->orders_status);
$criteria->compare('shipping_status',$this->shipping_status);
$criteria->compare('consignee',$this->consignee,true);
$criteria->compare('country',$this->country,true);
$criteria->compare('province',$this->province,true);
$criteria->compare('city',$this->city,true);
$criteria->compare('address',$this->address,true);
$criteria->compare('tel',$this->tel,true);
$criteria->compare('mobile',$this->mobile,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('shipping_id',$this->shipping_id,true);
$criteria->compare('shipping_name',$this->shipping_name,true);
$criteria->compare('pay_id',$this->pay_id,true);
$criteria->compare('pay_name',$this->pay_name,true);
$criteria->compare('goods_amount',$this->goods_amount,true);
$criteria->compare('add_time',$this->add_time,true);
$criteria->compare('confirm_time',$this->confirm_time,true);
$criteria->compare('pay_time',$this->pay_time,true);
$criteria->compare('shipping_time',$this->shipping_time,true);
$criteria->compare('pay_status',$this->pay_status);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return TblOrders the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}