Hi Alexander,
Sub-queries aren't still supported in PHQL, I suggest you the following options:
1. Using two queries
$maxIp = Geoip\Block::maximum(array(
'column' => 'ip_start',
'conditions' => 'ip_start <= ?0',
'bind' => array($start)
));
$blocks = Geoip\Block::find(array(
'conditions' => 'ip_start = ?0 AND ip_end >= ?1',
'bind' => array($maxIp, $end)
));
2. Using a RAW sql in your model
public static function getBlock($start, $end)
{
$sql = 'SELECT * FROM Geoip\Block
INNER JOIN (
SELECT MAX(ip_start) AS start
FROM Geoip\Block
) AS s ON (ip_start = s.start)
WHERE ip_end >= ?';
return $this
->getConnection()
->query($sql, array($start, $end))
->fetchAll();
}
However, I'm not sure if you really need to use the ORM here, since the maxmind database is mostly read-only, you can better implement a user component that provide this info querying directly to the database (same as above):
class GeoIp extends Phalcon\DI\Injectable
{
public function getBlock($start, $end)
{
$sql = 'SELECT * FROM Geoip\Block
INNER JOIN (
SELECT MAX(ip_start) AS start
FROM Geoip\Block
) AS s ON (ip_start = s.start)
WHERE ip_end >= ?';
return $this->db->query($sql, array($start, $end))->fetchAll();
}
}