1
我有一個命令連接到外部數據庫並將數據加載到我的應用程序的數據庫中。該命令將作爲cron作業定期運行。不過,我碰到以下問題,當我在控制檯中運行以下命令:在Symfony2命令中使用原則
PHP Fatal error: Call to undefined method Symfony\Component\Console\Application::getKernel() in E:\www\project\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand.php on line 43
我也跟着上的symfony的網站,以信教程here。
這裏的服務定義:
app.command.get_transactions:
class: AppBundle\Command\TransactionsCommand
arguments: [ @doctrine.orm.entity_manager ]
tags:
- { name: console.command }
這裏是我的命令代碼:
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use AppBundle\Entity\Transaction;
use AppBundle\Entity\TransactionSync;
use Doctrine\DBAL\DriverManager;
class TransactionsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('transactions:get')
->setDescription('Import transactions')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$q = $em->createQueryBuilder();
$q->select('t')->from('AppBundle:TransactionSync', 't')->orderBy('t.id', 'DESC')->setMaxResults(1);
$sync = $q->getQuery()->getResult();
$em1 = $this->getContainer()->get('doctrine')->getManager('rnr');
$conn = $em1->getConnection();
$query = "SELECT id, merchant, client, phone, traderTransIdent AS member_id, transaction_id, transaction_type_id, value AS amount, points, DATE_FORMAT(STR_TO_DATE(transaction_date, '%d-%m-%Y'), '%Y-%m-%d') AS transaction_date FROM merchant_transactions WHERE id > ". $sync->getId();
$stmt = $conn->prepare($query);
$stmt->execute();
$results = $stmt->fetchAll();
if(count($results) > 1)
{
$ts = new TransactionSync();
$ts->setStartTime(new \DateTime());
$id = 0;
foreach($results as $result)
{
$transaction_type = $em->getRepository('AppBundle:TransactionType')->find($result['transaction_type_id']);
$member = $em->getRepository('AppBundle:Member')->find($result['member_id']);
$transaction = new Transaction();
$transaction->setAmount($result['amount']);
$transaction->setPoints($result['points']);
$transaction->setClient($result['client']);
$transaction->setPhone($result['phone']);
$transaction->setTransactionId($result['transaction_id']);
$transaction->setTransactionDate(new \DateTime($result['transaction_date']));
$transaction->setTransactionType($transaction_type);
$transaction->setMember($member);
$em->persist($transaction);
$id = $result['id'];
}
$ts->setLastId($id);
$ts->setRecords(count($results));
$ts->setEndTime(new \DateTime());
$em->persist($ts);
$em->flush();
}
$output->writeln($text);
}
}
根據公認的答案網上here和其他許多地方,我所看到的,延長ContainerAwareCommand應該解決這個問題,但我仍然不斷收到錯誤。請協助指點我錯過了一步,我會很感激
你是如何定義你的命令的?即:您是將它放在Command命名空間中還是將其定義爲服務?你在使用全棧框架嗎? – user2268997
我正在使用全棧框架。我已經定義了這項服務。讓我編輯問題顯示這個 –