這是我第一次在這裏發帖,所以任何提示/建議,以改善我發佈總是感激。 (我不打算這是我的最後一篇文章:)]This-> get('foobar')給出「調用一個非對象的成員函數get()」Symfony2
我將我的控制器定義爲服務,所以我可以在需要時調用控制器內其他控制器的方法。到目前爲止,一切都很順利,但突然之間,我得到了'調用成員函數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)));
}