不使用yii框架从两个表的内连接获取数据库的值

时间:2014-05-26 10:12:08

标签: php yii

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

尝试获取非对象的属性

C:\瓦帕\ WWW \ jobsite_orginal \保护\模块\求职者\视图\站点\ advance_search_result.php(11)

作业表字段

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)
经验int(11)
contact_email varchar(100)
company_name varchar(100)
工资双倍 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 = 'jobs.title,  location.title, 
jobs.key_skills, jobs.description,   
 jobs.no_vacancy, jobs.experience,       jobs.company_name,
jobs.salary';
$criteria->join = 'INNER JOIN location ON jobs.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));
}

}

查看page-advance_search_result.php

<div >
 <h1>Search Results</h1>
 <ul style="list-style:none; ">
  <?php
foreach($model as $models)  
    { 
 $job_id=$models->id;
 ?>
 <li><p><?php //echo $models->title; ?>
 <?php echo CHtml::link($models->title,    
   array('site/advance_search_detail',
 'job_id'=>$job_id,'status'=>0));
  ?></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>

模型 - 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 :(得分:1)

我没有给你确切的解决方案,但我已经遇到过这类问题,并使用

解决了这个问题
$criteria->addInCondition

取代

$criteria->addCondition

请尝试一次。它可以解决您的问题。 或

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("(location.title like '%$title%' or  location.key_skills like '%$title%') and (jobs.experience like '%$experience%')");    
}
if($location!="")
{
   $criteria->addCondition("jobs.location_id like '%$location%'");
}
if($category!="")
{
   $criteria->addCondition("jobs.category_id like '%$category%'");
}
if($salary_min!="" && $salary_max!="")
    {
        $criteria->addCondition("jobs.salary >= '$salary_min' and jobs.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);

快乐编码。

答案 1 :(得分:0)

试试这个

$criteria = new CDbCriteria();
// instead of t specify the table name as such. For eg if the table involved is Job then

$criteria->select = 'jobs.title,  location.title,  jobs.key_skills, jobs.description, jobs.no_vacancy, jobs.experience, jobs.company_name, jobs.salary';
$criteria->join = 'INNER JOIN location ON jobs.location_id = location.id';