无法使用yii框架从内部联接获取数据库的值

时间:2014-05-23 05:59:05

标签: php yii

我无法使用带有内连接的yii框架从数据库中选择值。我的控制器是sitecontroller.php,视图文件是adavance_search_result.php。我收到了错误"

财产" Job.location"没有定义。

C:\ wamp \ www \ yii \ framework \ db \ ar \ CActiveRecord.php(145)"

工作表字段

id             int (100)            
user_id        int(100)         
posted_by      varchar(100) 
title          varchar(100) 
key_skills     varchar(200) 
no_vacancy     int(11)          
category_id    int(11)              
experience     int(11)          
contact_email  varchar(100)         
company_name   varchar(100)         
salary         double               
location_id    int int(100)

位置    1.id    2.title

我的控制器代码是

  public function actionadvance_search_result() {
     $title = $_GET['title'];
     $experience = $_GET['experience'];
     $location = $_GET['location'];
     $category = $_GET['category'];
     $salary_min = $_GET['salary_min'];
     $salary_max = $_GET['salary_max'];

     $criteria = new CDbCriteria();
     $criteria->select = 't.title,  location.title,  t.key_skills, t.description, t.no_vacancy, t.experience, t.company_name, t.salary';
     $criteria->join = 'INNER JOIN location ON t.location_id = location.id';
     if($experience != "") {
        $criteria->addCondition("(title like '%$title%' or  key_skills like '%$title%') and (experience like '%$experience%')");
     }
     if($location != "") {
        $criteria->addCondition("location_id like '%$location%'");
     }

     if($category != "") {
        $criteria->addCondition("category_id like '%$category%'");
     }
     if($salary_min != "" && $salary_max != "") {
        $criteria->addCondition("salary >= '$salary_min' and salary <= '$salary_max'");
     }

     $count = Job::model()->count($criteria);
     $pages = new CPagination($count);
     $pages->pageSize = 2;
     $pages->applyLimit($criteria);
     $model = Job::model()->findAll($criteria);

     $number_rec = count($model);
     if($number_rec <= 0) {
        $this->render('search_result1', array('model' => $model));
     }
     else {
        $this->render('advance_search_result', array(
                                                    'model' => $model,
                                                    'pages' => $pages
                                               ));
     }
  }

我的观看页面是

  <div>
     <div>
        <h1>Search Results</h1>
        <ul style="list-style:none; ">
           <?php
           foreach($model as $models) {
              $job_id = $models->id;
              ?>
              <li>
                 <p><?php echo CHtml::link($models->title); ?></p>
                 <p><?php echo CHtml::link($models->location->title); ?></p>
                 <p><?php echo $models->company_name; ?></p>
                 <p><?php echo $models->description; ?></p>
                 <p>Keyskill:<?php echo $models->key_skills; ?><p>
              </li>
              </br>
           <?php
           }
           ?>
        </ul>
        <p><?php $this->widget('CLinkPager', array(
                                                  'pages' => $pages,
                                             )) ?></p>
     </div>
  </div>

模型是job.php

代码

<?php

  class Job extends CActiveRecord
{
 public $title;
 public static function model($className=__CLASS__)
 {

return parent::model($className);
 }
public function tableName()
{
    return 'jobs';
}

public function rules()
{

return array(
          array('title,key_skills,description,no_vacancy,
category_id,experience,contact_email,company_name,company_name,salary,location_id','required'),
    );
}
public function relations()
  {
 return array(
'location' => array(self::HAS_MANY, 'location', 'id'),
 );
  }
public function attributeLabels()
{
    return array(
        'user_id' => 'User_ID',
        'title' => 'Title',
        'key_skills' => 'Key_skills',
    );
}
public function search()
{
    $criteria=new CDbCriteria;
    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->title);
    $criteria->compare('key_skills',$this->key_skills,true);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}
}

&GT;

模型 - location.php

 <?php
 class Location extends CActiveRecord
 {
  public $title;
 public $Ltitle;
 public static function model($className=__CLASS__)
  {
 return parent::model($className);
  }
public function tableName()
{
    return 'location';
}

 public function rules()
{
    return array(
        array('title,description','required'),
    );
}
public function relations(){
   return array(
'job' => array(self::BELONGS_TO, 'Job', 'id'),
);    
}

}
?>

有人帮帮我吗?

2 个答案:

答案 0 :(得分:0)

添加然后查看是否有效

$criteria->with = array(
    'location'
);

答案 1 :(得分:0)

Job和Location表有关系吗?  如果是,那么为什么不在yii中使用该关系。与您相比,它非常易于使用。所以我的建议是,如果表具有主键和外键之间的关系,那么在yii中使用Gii模块创建模型。

之后你只做

$criteria = new CDbCriteria();
$criteria->select = array('t.title,location.title as ltitle');
$criteria->with = array('location');
if($location != "") {
 $criteria->compare('t.theField_name' , $someThing);
}
$getValues = Job::model()->findAll($criteria);

foreach($getValues as $val){
    echo $val->location->title;//title from location table.
    echo $val->title;//title from job table.
}

您还在工作中添加了一些内容,下面的位置模型是详细信息。

首先,将以下代码添加到作业模型文件

//Job Model.
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(
    'location' => array(self::HAS_MANY, 'Location', 'id'),
);
}

然后将以下代码添加到您的位置模型文件

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(
 'job' => array(self::BELONGS_TO, 'Job', 'id'),
  );    
}

并确保位置表中已设置主键和在作业表location_id中的id字段作为外键。

希望这会对你有所帮助。 :)