1
I need to transfer the PDO query below to DOCTRINE:
$query = $con->query('SELECT *,
( 6371 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) ) AS distance, city_name
FROM event INNER JOIN city ON event_city_id = city_id
WHERE event_sin_active = 1
ORDER BY distance');
So far I’ve been able to rewrite part of this query in Doctrine. Problem appears when inserting into SELECT a Haversine formula according to the questions: SQL search using Haversine in Doctrine and How to calculate Distance using latitude and longtitude?
The two tables in the database used are Event and City
CREATE TABLE `event` (
`event_id` int(11) NOT NULL,
`event_name` varchar(45) NOT NULL,
`event_image` varchar(45) DEFAULT NULL,
`event_city_id` int(11) NOT NULL,
`latitude` double DEFAULT NULL,
`longitude` double DEFAULT NULL,
`event_tax_service` decimal(10,2) DEFAULT NULL,
`event_user_id` int(11) NOT NULL,
`event_sin_active` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `city` (
`city_id` int(11) NOT NULL AUTO_INCREMENT,
`city_name` varchar(100) NOT NULL,
PRIMARY KEY (`city_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
When trying to execute the query below, the system returns a blank screen and does not indicate which Exception. What is the correct way to write this query?
$qb = $entityManager->createQueryBuilder();
$qb->select("e, e.eventCityId,c.cityName, (6371 * ACOS(SIN(RADIANS($latitude)) * SIN(RADIANS(e.latitude)) + COS(RADIANS($latitude)) * COS(RADIANS(e.latitude)) * COS(RADIANS(e.longitude) - RADIANS($longitude)))) as distance")
->from('\App\Entity\Event', 'e')
->innerJoin('\App\Entity\City', 'c', Join::WITH, $qb->expr()->eq('e.eventCityId', 'c.cityId'))
->where('e.eventSinActive = 1')
->orderBy('distance', 'ASC');
It would really help you to get the question wrong. If you do not know how to display the error, it is the case of searching if you no longer have a solution on the site, or open a specific question about it. Before, see the PHP error log. The problem may not be in the query itself, but in the way you pass the variables or in the rest of the syntax. Instead of doing select, switch to an echo and see the exit. Take a look at the Doctrine documentation that there is likely to be a way to display the errors. Also, you have the Mysql and PHP log to check. But a simple echo solves in these cases instead of running.
– Bacco
As relevant as the database structure are the entity classes that map the bd structure into PHP objects.
– Vinícius Fagundes