(你面前的文字来自在线翻译)。
上下文:开发一个Symfony命令,在数据库中检索没有GPS坐标的本地化表的地址(字段纬度和经度为空的本地化)。通过Google API(谷歌地理编码)找到与地址对应的GPS坐标,然后在数据库中插入坐标。
代码文件:
LocationCommand.php
<?php
namespace Keosu\DataModel\LocationModelBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Keosu\DataModel\LocationModelBundle\Utils\LocationManager;
class LocationCommand extends ContainerAwareCommand
{
private $locationManager;
public function __construct(LocationManager $locationManager)
{
parent::__construct();
$this->locationManager = $locationManager;
}
protected function configure()
{
$this
->setName('keosu:coordinated')
->setDescription('Populate table location with google coordinated , if the field long and lat are empty')
->setHelp('For call this command enter app/console Keosu:coordinated ')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getDoctrine()->getManager();
$apiUrl = "http://maps.googleapis.com/maps/api/geocode/json?";
$apiKey = $this->getContainer()->getParameter('google_api_key');
//My collection of location
$locations = $this->locationManager->findAll();
foreach ($locations as $location)
{
if ($location->getLat() == null && $location->getLng() == null)
{
$address = $location->getAddress();
$address .= ',' . $location->getPostalCode();
$urlAddress = preg_replace('/\s+/', '+', $address);
$geolocUrl = $apiUrl.'address='.$urlAddress.'&key='.$apiKey;
//Query to send for Geocoding API
$query = sprintf($geolocUrl,urlencode(utf8_encode($address)));
$result = json_decode(file_get_contents($query));
$json = $result->results[0];
$address->setLat($json->geometry->location->lat);
$address->setLng($json->geometry->location->lng);
$em->persist($address);
$em->flush();
}
}
}
}
我用于命令的服务:LocationManager.php
<?php
namespace Keosu\DataModel\LocationModelBundle\Utils;
use Doctrine\ORM\EntityManager;
class LocationManager
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function findAll()
{
//Find all location
return $this->em->getRepository('LocationModelBundle:Location')->findAll();
}
}
问题:在控制台中我输入&#34; app / console keosu:coordinated&#34;为了启动我的命令,控制台显示以下错误:
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "keosu:coordinated" is not defined.
Did you mean this?
keosu:export
keosu:export是另一个目录中的命令。该命令位于此目录中(Keosu / CoreBundle / Command / ExportCommand.php)。
我确实检查了命名空间,但错误仍然存在。我甚至不知道我的代码是否会处理我的请求,因为命令没有启动。
以下是该应用程序的架构。