findbysql searching 2 tables
i am using this code to search 2 tables where $id is a variable
$id=$_REQUEST['company_id'];
$result=User::model()->findBysql('select a.email from user a , recruiter b where a.id=b.user_id and b.company_id=$id');
i am getting error this code is not开发者_开发技巧 working any ideas please
1) Never user $_REQUEST. You could either user Yii::app()->request->getParam('company_id') or search in respectively $_GET/$_POST
2)
$result = User::model()->find(array(
'condition' => 'r.company_id=:company_id',
'join' => 'INNER JOIN recruiter r ON t.id = r.user_id',
'params' => array(
':company_id' => $id
)
));
3) You probably want to define the recruiter as User's model relation
The main problem with the code you've written here is that your sql string in enclosed with single quotes. But I don't know if it's a typo here or it's the actual problem. Could you please give us the error message?
I got help from yii live chat they told me because id is variable the sql query should be in double quotes and id in {}
$result = User::model()->findBysql("select a.email from user a , recruiter b where a.id=b.user_id and b.company_id={$id}");
Thanks for the answers
精彩评论