Yii:如何通过1个表字段创建2个输入字段?

时间:2012-03-23 10:57:58

标签: php yii

我需要:
 form.created_fromform.created_to =&gt; table.created BETWEEN <form.created_from> AND <form.created_to>

#table model
class SomeModel extends CActiveRecord {
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'created' => 'Created',
        );
    }
}
#form model
class SomeModelForm extends CFormModel {
    public $id;
    public $createdFrom;
    public $createdTo;
}
#controller
class SomeController extends Controller {
    public function actionSomeAction() {
       $model = SomeModel('SomeModelForm');
    }
}

错误:异常:未定义属性“SomeModel.createdFrom”。
如果不在createdFrom中定义无用的属性(createdToSomeModel),是否可以这样做?

2 个答案:

答案 0 :(得分:1)

你可以尝试这样,我希望它有效:

$model = new SomeModelForm;
$model->attributes = $_POST['SomeModelForm'];

$criteria = new CDbCriteria();
$criteria->addBetweenCondition('created', $model->createdFrom, $model->createdTo);
$result = SomeModel::model()->findAll($criteria);

答案 1 :(得分:0)

#form model
class SomeModelForm extends CFormModel {
    public $id;
    public $createdFrom;
    public $createdTo;
    public function search()
    {
        $criteria = new CDbCriteria();
        $criteria->compare('id', $this->id);
        $criteria->addBetweenCondition('created', $this->createdFrom, $this->createdTo);
        return new CActiveDataProvider(SomeModel::model(), array(
            'criteria' => $criteria,
        ));
    }
}

#controller
class SomeController extends Controller {
    public function actionSomeAction() {
       $model = SomeModelForm();
    }
}