Reputation: 91
I cant get the value selected from database using yii framework with inner join.My controller is sitecontroller.php and view file is adavance_search_result.php.I got the error "
Trying to get property of non-object
C:\wamp\www\jobsite_orginal\protected\modules\jobseeker\views\site\advance_search_result.php(11)
jobs table fields
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)
location table
1.id
2.title
sitecontroller code
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));
}
}
View 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>
Model-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,
));
}
}
?>
Model 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'),
);
}
}
?>
Upvotes: 0
Views: 492
Reputation: 1053
Try this
$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';
Upvotes: 0
Reputation: 693
I am not giving you the exact solution but I have already faced this type of problem and solved it by using
$criteria->addInCondition
in place of
$criteria->addCondition
Please try this once. It may solve your problem. or
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);
Happy Coding.
Upvotes: 1