codeigniter活动记录和mysql

时间:2010-06-06 18:26:30

标签: php mysql activerecord codeigniter join

我在我的codeigniter应用程序的模态中运行Active Record查询,查询看起来像这样,

public function selectAllJobs()
{
    $this->db->select('*')
             ->from('job_listing')
             ->join('job_listing_has_employer_details', 'job_listing_has_employer_details.employer_details_id = job_listing.id', 'left');
             //->join('employer_details', 'employer_details.users_id = job_listing_has_employer_details.employer_details_id');

    $query = $this->db->get();
    return $query->result_array();
}

这将返回一个如下所示的数组

    [0]=>
  array(13) {
    ["id"]=>
    string(1) "1"
    ["job_titles_id"]=>
    string(1) "1"
    ["location"]=>
    string(12) "Huddersfield"
    ["location_postcode"]=>
    string(7) "HD3 4AG"
    ["basic_salary"]=>
    string(19) "£20,000 - £25,000"
    ["bonus"]=>
    string(12) "php, html, j"
    ["benefits"]=>
    string(11) "Compnay Car"
    ["key_skills"]=>
    string(1) "1"
    ["retrain_position"]=>
    string(3) "YES"
    ["summary"]=>
    string(73) "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
    ["description"]=>
    string(73) "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
    ["job_listing_id"]=>
    NULL
    ["employer_details_id"]=>
    NULL
  }
}

job_listing_id和employer_details_id返回NULL但是如果我在phpmyadmin中运行SQL我得到完整的结果集,我在phpmyadmin中运行的查询是,

    SELECT *
FROM (
`job_listing`
)
LEFT JOIN `job_listing_has_employer_details` ON `job_listing_has_employer_details`.`employer_details_id`
LIMIT 0 , 30

我有没有理由得到不同的结果?

1 个答案:

答案 0 :(得分:3)

运行$ this-> db-> last_query()并检查差异。这是一个非常有用的调试工具。

当然限制会产生影响,但我忽略了这一点。

在phpmyadmin的查询中,你的'ON'没有第二部分。 我认为你在phpmyadmin中的查询应该是:

SELECT *
FROM (
`job_listing`
)
LEFT JOIN `job_listing_has_employer_details` ON `job_listing_has_employer_details`.`employer_details_id` =  = `job_listing`.`id`

你应该使用result_array,尽管对方说的话。