PHP-Codeigniter活动记录和MySQL
作者:互联网
我在我的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和loyal_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
我得到不同结果的原因是什么?
解决方法:
运行$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.
标签:activerecord,codeigniter,join,mysql,php 来源: https://codeday.me/bug/20191210/2098515.html