gpt4 book ai didi

Doctrine 距离函数

转载 作者:行者123 更新时间:2023-12-02 04:33:05 26 4
gpt4 key购买 nike

我尝试使用一个函数来计算 $user lat/lng 和我的 BDD 中距离有限的坐标之间的距离。

这是一个SQL请求,我尝试用Doctrine来实现。

这是我的代码

    $config = new \Doctrine\ORM\Configuration();

$config->addCustomNumericFunction('COS', 'DoctrineExtensions\Query\Mysql\Cos');

$config->addCustomNumericFunction('ACOS', 'DoctrineExtensions\Query\Mysql\Acos');

$config->addCustomNumericFunction('RADIANS', 'DoctrineExtensions\Query\Mysql\Radians');

$config->addCustomNumericFunction('SIN', 'DoctrineExtensions\Query\Mysql\Sin');

$maxLat = $form_citylat + rad2deg($rad / $R);
$minLat = $form_citylat - rad2deg($rad / $R);
$maxLng = $form_citylng + rad2deg(asin($rad / $R) / cos(deg2rad($form_citylat)));
$minLng = $form_citylng - rad2deg(asin($rad / $R) / cos(deg2rad($form_citylat)));


$qb = $this->createQueryBuilder('u')->select('u.lat, u.lng')
->addSelect('acos(sin(:lat)*sin(radians(u.Lat)) + cos(:lat)*cos(radians(u.Lat))*cos(radians(u.lng)-:lng)) * :R As D')
->where('lat Between :minlat And :maxlat And lng Between :minlng And :maxlng And acos(sin(:lat)*sin(radians(u.Lat)) + cos(:lat)*cos(radians(u.Lat))*cos(radians(u.Lng)-:lng)) * :R < :rad')
->setParameter('lat',deg2rad($form_citylat))
->setParameter('lng',deg2rad($form_citylng))
->setParameter('minlat',$minLat)
->setParameter('minlng',$minLng)
->setParameter('maxlat',$maxLat)
->setParameter('maxlng',$maxLng)
->setParameter('rad',$rad)
->setParameter('R',$R)
->orderBy('D');

return $qb->getQuery()->getResult();`

但是我收到了这个错误信息:

[Syntax Error] line 0, col 40: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '.'

我尝试了不同的选项,但都不起作用。

有人知道吗?

最佳答案

public function findByThemeAndDistance($theme,$distance,$user){     

$latUser=$user->getAddress()->getLnt();
$lngUser = $user->getAddress()->getLgt();

return $this->createQueryBuilder('a')
//->select('a as activity,dist('.$distance.') as distance' )
->join('a.author','u')
->join('u.address','add')
->andWhere('a.theme=:val')
->andWhere( '(6378 * acos(cos(radians( add.lnt)) * cos(radians(' . $latUser . ')) * cos(radians(' . $lngUser . ') - radians(add.lgt)) + sin(radians(add.lnt )) * sin(radians(' . $latUser . '))))< :distance')
->setParameter('distance', $distance)
->setParameter('val',$theme)
->orderBy( 'a.author','ASC')
->getQuery()
->getResult();
}

关于 Doctrine 距离函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com