0

我必须将名称查找为名字和姓氏的组合,即 fname 和 lname 的 concat 并返回所有可能的匹配项。现在,通过查看这篇文章,我已经能够在这方面取得很大的成功。

现在我有我的模型类,我可以在其中搜索学生姓名,如下所示:

class StudentRegistration extends CActiveRecord
{

    /**
     * Public Varibale that defines the full name 
     */

     public $full_name;


     //my search function has something like this
     public function search()
     {
          $criteria=new CDbCriteria;
          $criteria->addSearchCondition('concat(fname, " ", lname)', $this->full_name); 
     .
     .
     .

     //my rules defining this
     full_name', 'safe', 'on'=>'search'),

    //my getter method like this
    public function getFull_Name()
    {
            return $this->fld_fname.' '.$this->fld_lname;
    }

}

使用这种结构,我已经能够将全名属性添加到我的视图中,并且能够在我的表单视图中搜索全名。

现在这是我完全难过的部分。我正在尝试访问我定义的此搜索条件,以在具有不同模型的另一个视图中搜索 ajax 函数中的全名。

在那里,我为 name 字段定义了一个变量,并分配了一个控制器来返回来自 studentReg 模型搜索的搜索结果。但我没有成功(如果我设置断点抱怨手表存在,我的 netbeans/xdebug 会毫不客气地崩溃,而我没有)

这是我的搜索动作类

  class StudentRegChecklistController extends Controller
  {
      .
      .
      .
      .
      .
        public function actionSearchStudent()
        {
        if(Yii::app()->request->getIsAjaxRequest() && isset($_POST["autoField"]))
        {
            $models=  StudentSearch::searchStudent($_POST["autoField"]);                
            $criteria=new CDbCriteria;
            $criteria->compare("full_name",$_POST["autoField"],true);
            $models=  StudentRegistration::model()->findAll($criteria);
            foreach($models as $model)
            {
              echo "<option value=$model->gno style=\"font-size:18px;cursor:pointer\">".$model->fname." ".$model->lname."</option>";
              echo "</br>";
            }
        }

我在控制器中做错了什么?试图实现这样的目标

4

1 回答 1

1

看起来你正在使用StudentRegistration::model()->findAll($criteria)而不是StudentRegistration::model()->search()

第一种方法将使用$criteria信息查看您的模型表,并尝试匹配相关的表列。但 *full_name* 不是表列。

search()方法使用$ this作为过滤器。$thisStudentRegistration实例,*$this->full_name* 是已知属性,search()知道如何处理它。

如果要添加更多过滤器,只需在用作过滤器的实例中设置相关属性即可。就像是 :

$filter = new StudentRegistration();
$filter->setAttributes(array('full_name' => "John Doe", 'registration_date' => '2013-01-01'));
$results = $filter->search();

注意:您可能希望使用由 CActiveRecord 处理的大量分配,而不是setAttributes(),前提是您的所有输入都是“搜索安全”:

$filter->attributes = $my_array_of_parameters; // likely $_POST['something']

如果它们不是“搜索安全”,请将 setAttributes() 的第二个参数设置为 false。或使用setAttribute() ...

于 2013-05-20T18:48:47.130 回答