简体   繁体   中英

Get DISTINCT value from related model

I have 2 models. Users and Ips . Users relation :

return array(
    'ips' => [self::HAS_MANY, 'Ips', ['user_id' => 'id']]
);

Ips model has fields id, user_id, ip .

I need to get all Users with its unique Ips .

$users = $model->with(['ips' => [
    'select' => 'ip', 
    'distinct' => true
]])->findAll();

doesn`t work the way I need. Probably it gets unique by field id .

Thanks for response!

尝试像这样定义您的关系:

return array( 'ips' => [self::HAS_MANY, 'Ips', 'user_id'] );

You may want to try GROUP BY

$users = User::model()->with(array(
    'ips'=>array(
        'group'=>'user_id, ip',
    )
))->findAll();

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM