hasOne和hasMany的区别,单从getLastSql()获取到的SQL代码看不出区别,区别在返回的数据的数据结构 hasOne
获取1条,当有多条时选取最顶部或最底部可以用ORDER BY `last_follow_date` DESC/ASC"控制
$sql = user1Model::with(['userToRecord'=>function($q){
$q->field('id,user_id,agent_id,last_follow_date')
->order('last_follow_date asc');
}])
$ids2 = $sql->select();
var_dump($sql->getLastSql());
SELECT `id`,`user_id`,`agent_id`,`last_follow_date` FROM `hl_user_record_log` WHERE `user_id` IN (140488,140487,140486,140485,140484) ORDER BY `last_follow_date` ASC
[0]=>
array(4) {
["id"]=>
int(140488)
["id_card"]=>
string(18) ""
["create_time"]=>
string(19) "2022-07-27 17:32:45"
["user_to_record"]=>
array(4) {
["id"]=>
int(65)
["user_id"]=>
int(140488)
["agent_id"]=>
int(122433)
["last_follow_date"]=>
string(19) "2022-07-28 10:54:52"
}
}
hasMany
获取多条
$sql = user1Model::with(['userToRecords'=>function($q){
$q->field('id,user_id,agent_id,last_follow_date')
->order('last_follow_date asc');
}])
$ids2 = $sql->select();
var_dump($sql->getLastSql());
[0]=>
array(4) {
["id"]=>
int(140488)
["id_card"]=>
string(18) ""
["create_time"]=>
string(19) "2022-07-27 17:32:45"
["user_to_records"]=>
array(2) {
[0]=>
array(4) {
["id"]=>
int(64)
["user_id"]=>
int(140488)
["agent_id"]=>
int(122433)
["last_follow_date"]=>
string(19) "2022-07-27 19:11:49"
}
[1]=>
array(4) {
["id"]=>
int(65)
["user_id"]=>
int(140488)
["agent_id"]=>
int(122433)
["last_follow_date"]=>
string(19) "2022-07-28 10:54:52"
}
}
}