这是我第一次在这里发帖,因此任何有关改善发布的提示/建议都会受到赞赏。 (我不打算把它作为我的最后一篇文章:])
我将控制器定义为服务,因此我可以在需要时调用控制器内其他控制器的方法。到目前为止,一切都进展顺利,但突然之间,我得到了一个非对象的成员函数get()调用'错误。我完全不知道它可能是什么,因为它在其他多个场合工作..
我现在只与Symfony2合作6周了,所以当你看到它时,请原谅写得不好的代码。
这是生成错误的方法:
public function generate1PDF($customer)
{
if (file_exists('/pdf/' . $customer->getGoogleViewID() . '/' . date("Y") . '/' . date("Y-m") . '.pdf')) {
//PDF already exists. Delete it (because generating a new one will have more recent data)
unlink('/pdf/' . $customer->getGoogleViewID() . '/' . date("Y") . '/' . date("Y-m") . '.pdf');
}
//THIS IS THE LINE THAT THROWS THE EXCEPTION
$arr = $this->get('itr.login.report.controller')->prepareReport($customer);
if ($arr instanceof Response) {
return $arr;
}
$data = $arr['data'];
$report = $arr['report'];
$this->get('knp_snappy.pdf')->generateFromHtml(
$this->renderView(
'itrLoginBundle:Report:report.html.twig',
array(
'report' => $data,
'klant' => $customer,
'rapport' => $report,
'cmd' => false
)
),
'/pdf/' . $customer->getGoogleViewID() . '/' . date("Y") . '/' . date("Y-m") . '.pdf'
);
$this->reportRepository->updateGenerated($report);
}
这是抛出异常的行的方法,应该导航到:
public function prepareReport($customer, $report = null)
{
if ($report == null) {
$reportUitDB = $this->reportRepository->findLatestReportByCustomerId($customer->getId());
} else {
$reportUitDB = $report;
}
if ($reportUitDB == null) {
return null;
} else {
$data = json_decode($reportUitDB->getData(), 'json');
return array('data' => $data, 'report' => $reportUitDB);
}
}
这是我的services.yml文件:
services:
itr_login.form.type.customer:
class: itr\LoginBundle\Form\Type\CustomerType
tags:
- { name: form.type, alias: customer }
itr.twig.timeConvert_extension:
class: itr\LoginBundle\Twig\timeConvertExtension
tags:
- { name: twig.extension }
itr.login.default.controller:
class: itr\LoginBundle\Controller\DefaultController
itr.login.customer.controller:
class: itr\LoginBundle\Controller\CustomerController
itr.login.mail.controller:
class: itr\LoginBundle\Controller\MailController
itr.login.pdf.controller:
class: itr\LoginBundle\Controller\PdfController
itr.login.report.controller:
class: itr\LoginBundle\Controller\ReportController
customer_repository:
class: itr\LoginBundle\Repository\customerRepository
arguments:
dbal: "@doctrine.dbal.default_connection"
report_repository:
class: itr\LoginBundle\Repository\reportRepository
arguments:
dbal: "@doctrine.dbal.default_connection"
token_repository:
class: itr\LoginBundle\Repository\tokenRepository
arguments:
dbal: "@doctrine.dbal.default_connection"
一个有效的例子:
try {
$customer = $this->customerRepository->findFirstWithMoreThan30DaysAndActiveAndStatusAndMailday(date('j'));
if ($customer != null) {
$this->customerRepository->updateStatus($customer, 5);
//haal data op en stop ze in databank
$tijd = new \DateTime();
$report = new Report(null, null, null, null, $customer, $tijd->format("Y-m-d H:i:s"));
$report->setData($analytics);
$this->reportRepository->insertReport($report);
$this->customerRepository->updateStatus($customer, 10);
//genereer PDF
//HERE IS THE EXAMPLE LINE, that IS working properly
$this->get('itr.login.pdf.controller')->generate1PDF($customer);
$this->customerRepository->updateStatus($customer, 20);
//stuur mail
$this->get('itr.login.mail.controller')->send1Mail($customer);
$this->customerRepository->updateStatus($customer, 30);
$this->customerRepository->updateStatus($customer, 0);
$tweet = 'De klant is succesvol behandeld!';
} else {
$tweet = 'Er is geen enkele klant gevonden waarvan: het laatste rapport meer dan 30 dagen geleden opgehaald is, er momenteel geen rapport in verwerking is, de huidige status actief is, én de huidige dag overeen komt met de door de klant aangegeven dag om te mailen.';
}
return $this->redirect($this->generateUrl('mainPage', array('tweet' => $tweet)));
}
答案 0 :(得分:0)
我找到了一个解决方案,所以我想我会在这里发布给未来的读者:
有人向我指出,我不应该只为每个控制器提供服务,因此我可以从任何控制器访问所有方法,因为这是一种不好的做法。
所以我遵循了我同事的建议并构建了一个包含'generate1PDF'和'prepeareReport'方法的服务。我可以从任何控制器调用这些方法(假设我将服务注入其中),所以我的结构已经改进,我的问题已经解决了。