在 null zendframework 3 上调用成员函数 hello()

Call to member function hello() on null zendframework 3

我遇到错误

Call to a member function buildCTCCompensationData() on null

in PayrollspendController.php 这行代码:

 $compData = $this->payrollspendManager->hello($postData);

我已经检查了 module.config.php 和 __construct() 方法,但找不到错误。代码中的问题是什么? 请帮我解决错误
我已经正确调用了方法
如果需要任何代码将会有所帮助
如果想要查看也会提供
的文件 懂zendframework的帮我解决问题

这是module.config.php

        'dashboard_activity_payrollspend' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/dashboard/employer-details/activity-manage',
                            'constraints' => [
                                'action' =>'[a-zA-Z][a-zA-Z0-9_-]*',
                                'id' => '[a-zA-Z0-9_-]+',
                            ],
                            'defaults' => [
                                'controller' => Controller\PayrollspendController::class,
                                'action' => 'add',

                            ],
                        ],
                    ],
    'controllers' => [
            'factories' => [

              Controller\PayrollspendController::class => Controller\Factory\PayrollspendControllerFactory::class
            ],
        ],
        'service_manager' => [
            'factories' => [
                Service\PayrollspendManager::class => Service\Factory\PayrollspendManagerFactory::class
            ],
        ],

这是我的控制器PayrollspendController.php

<?php 
namespace Dashboard\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Application\Entity\PyPayGroup;
use Application\Entity\PyPayPeriod;
//use Payroll\Form\SalaryVariationForm;
use Zend\Session\SessionManager;
use Application\Entity\CommonCompanyHeader;
use Dashboard\Form\PayrollspendForm;
class PayrollspendController  extends AbstractActionController
{
 private $entityManager;
 private $sessionContainer;
 private $pyPayPeriodClass;
 private $pyPayGroupClass;
 private $companyClass;
  public function __construct($entityManager)
 {
 $this->entityManager = $entityManager;
 $this->pyPayGroupClass = $this->entityManager->getRepository(PyPayGroup::class);
 $this->pyPayPeriodClass = $this->entityManager->getRepository(PyPayPeriod::class);
 $this->companyClass = $this->entityManager->getRepository(CommonCompanyHeader::class);
 $this->commonTranslationManager = $commonTranslationManager;
 $sessionManager = new SessionManager();
 $this->sessionContainer = new Container('ContainerNamespace', $sessionManager);
 $arrLabelId =  [];
}
 public function addAction()
  {
        if ($this->sessionContainer->empId == "") {
            return $this->redirect()->toRoute('admin_user_login');
        }
        if (!in_array('PY', $this->sessionContainer->arrRole)) {

            if (!in_array('py_admin', $this->sessionContainer->arrRole)) {

                return $this->redirect()->toRoute('dashboard_ess_index');
            }
        }
        $reportForm = new PayrollspendForm();
        $payGroup = $this->pyPayGroupClass->findBy([
            'ouCode' => $this->sessionContainer->ouCode,
            'langCode' => $this->sessionContainer->langCode,
            'pgActive' => 1
        ]);
        $reportForm->buildPayGroupData($payGroup);
        $company = $this->companyClass->findBy([
            'ouCode' => $this->sessionContainer->ouCode,
            'langCode' => $this->sessionContainer->langCode
        ]);
        $reportForm->buildCompanyData($company);
        $payPeriodData = ['' => 'Select'];
        $reportForm->get('payPeriod')->setValueOptions($payPeriodData);
        $postData = $this->getRequest()->getPost()->toArray();
       $postData['ouCode'] = $this->sessionContainer->ouCode;
       $postData['langCode'] = $this->sessionContainer->langCode;
       $compData = $this->payrollspendManager->hello($postData);
        $groupByData = [
            '' => 'Select',
            'location' => 'Location',
            'department' => 'Department',
            'cost-center' => 'Cost center'
       ];
        $reportForm->get('groupby')->setValueOptions($groupByData);
        return new ViewModel([
            'reportData' => $compData,
            'form' => $reportForm,
            'ouCode' => $this->sessionContainer->ouCode,
            'postData' => $postData,
            'langCode' => $this->sessionContainer->langCode,
            'arrLabels' => $this->arrLabels
        ]);
        $resultData->setTerminal(true);
        return $resultData;
    }
}

这是PayrollspendControllerFactory.php

<?php
namespace Dashboard\Controller\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Dashboard\Service\PayrollspendManager;
//use Application\Service\CommonTranslationManager;
use Dashboard\Controller\PayrollspendController;
class PayrollspendControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container,$requestedName, array $options = null)
    {
        $entityManager = $container->get('doctrine.entitymanager.orm_default');
        $payrollspendManager = $container->get(PayrollspendManager::class);
        //$hrManager = $container->get(HrManager::class);
     //   $commonTranslationManager = $container->get(CommonTranslationManager::class);

        return new PayrollspendController($entityManager);
    }
}

这个PayrollspendManager.php

<?php
namespace Dashboard\Service;
//use Application\Entity\DsAnnouncement;
//use Application\Entity\TmDomain;

// The AnnouncementManager service is responsible for adding new task.
class PayrollspendManager
{

    /**
     * Doctrine entity manager.
     * @var Doctrine\ORM\EntityManager
     */
    private $entityManager;

    // Constructor is used to inject dependencies into the service.
    public function __construct($entityManager)
    {
        $this->entityManager = $entityManager;
    }
    public function hello($postData)
    {

        $headerData = $this->getAllCTCCompensationHeader($postData);
        $compData = $this->getAllCTCCompensationData($postData);

        return [
            'header' => $headerData,
            'detail' => $compData
        ];
    }

    public function getAllCTCCompensationHeader($postData)
    {
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder->select('DISTINCT(pc.pyComPayitem) as payHeader')
                ->from(PyCompensationDetails::class, 'pc')
                ->innerJoin(PyPayItemPaygroupMap::class, 'ppipm', 'with', 'ppipm.payitemCode = pc.pyComPayitem
                       AND ppipm.ouCode = pc.ouCode 
                       AND ppipm.langCode= pc.langCode 
                       AND ppipm.pgCode= pc.pgCode')
                ->where('pc.ouCode = ?1')
                ->andWhere('pc.langCode = ?2')
                ->andWhere('pc.pgCode = ?3')
                ->andWhere('pc.isModified = ?4')
                ->setParameter('1', $postData['ouCode'])
                ->setParameter('2', $postData['langCode'])
                ->setParameter('3', $postData['payGroup'])
                ->setParameter('4', 0)
                ->orderBy('ppipm.orderofProcessing', 'ASC');
        $compData = $queryBuilder->getQuery()->getResult();
        $headerData = [
            '0' => 'Employee Id',
            '1' => 'Employee Name'
        ];
        foreach ($compData as $compHeader) {
            $headerData[] = $compHeader['payHeader'];
        }
        $headerData[] = 'PF';
        $headerData[] = 'ESIC';
        $headerData[] = 'Total';

        return $headerData;
    }

    /**
     * Get compensation detail data
     * 
     * @param type $postData
     * @return type
     */
    public function getAllCTCCompensationData($postData)
    {
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder->select('pc.pyComEmpid, pc.amount, pc.pyComPayitem, pi.payitemDesc, hn.empFname, hn.empMname, hn.empLname')
                ->from(PyCompensationDetails::class, 'pc')
                ->innerJoin(HrEmpid::class, 'he', 'with', 'pc.ouCode = he.ouCode 
                    and pc.langCode = he.langCode
                    and pc.pyComEmpid = he.empId'
                )
                ->innerJoin(HrEmpName::class, 'hn', 'with', 'pc.ouCode = hn.ouCode 
                    and pc.langCode = hn.langCode
                    and pc.pyComEmpid = hn.empId'
                )
                ->innerJoin(PyPayItem::class, 'pi', 'with', 'pc.ouCode = pi.ouCode 
                    and pc.langCode = pi.langCode
                    and pc.pyComPayitem = pi.payitemCode'
                )
                //->innerJoin(SmartlistData::class,'sd','with','sd.dataCode = pi.smartlistPayitemtype'
                 //)
                ->leftJoin(PyPayItemPaygroupMap::class, 'ppipm', 'with', 'ppipm.payitemCode = pi.payitemCode '
                        . 'AND ppipm.ouCode = pi.ouCode '
                        . 'AND ppipm.langCode= pi.langCode '
                        . 'AND ppipm.pgCode= pc.pgCode')
                ->where('pc.ouCode = ?1')
                ->andWhere('pc.langCode = ?2')
                ->andWhere('pc.pgCode = ?3')
                ->andWhere('pc.isModified = ?4')
                ->andWhere('he.smartlistEmpstatus != ?5')
                ->andWhere('pi.smartlistPayitemtype IN (94,99,100)')
                ->orderBy('ppipm.orderofProcessing', 'ASC')
                ->setParameter('1', $postData['ouCode'])
                ->setParameter('2', $postData['langCode'])
                ->setParameter('3', $postData['payGroup'])
                ->setParameter('4', 0)
                ->setParameter('5', 56);
               // ->orderBy('pc.pyComEmpid');
               // echo  $queryBuilder->getQuery()->getSQL();
               //exit;

        $compData = $queryBuilder->getQuery()->getResult();
       // echo  '<pre>';
        //print_r($compData);
       // exit;

        $data = [];


        if (!empty($compData)) {
            $total = 0;
            foreach ($compData as $dataC) {



        $data[$dataC['pyComEmpid']]['Employee Id'] = $dataC['pyComEmpid'];
        $data[$dataC['pyComEmpid']]['Employee Name'] = sprintf('%s %s %s', $dataC['empFname'], $dataC['empMname'], $dataC['empLname']);
        $data[$dataC['pyComEmpid']][$dataC['pyComPayitem']] = $dataC['amount'];
        $statData = $this->getStatuoryData($postData, $dataC['pyComEmpid']);
        //echo '<pre>';
       // print_r($statData);
        //exit;



                if(isset($statData['pf']) && ($statData['pf'] == 1)){
                    $parameterData = $this->getParamaterData($postData, 'pf', $dataC['pyComEmpid']);
                    $data[$dataC['pyComEmpid']]['PF'] = $this->getPFData($postData, $parameterData);
                } else {
                    $data[$dataC['pyComEmpid']]['PF'] = 0;
                }
                if(isset($statData['esic']) && ($statData['esic'] == 1)){
                    $parameterData = $this->getParamaterData($postData, 'esic', $dataC['pyComEmpid']);
                    $data[$dataC['pyComEmpid']]['ESIC'] = $this->getESICData($postData, $dataC['pyComEmpid']);
                } else {
                    $data[$dataC['pyComEmpid']]['ESIC'] = 0;
                }


                $data[$dataC['pyComEmpid']]['Total'] = $this->getCTCCompensationSum($postData, $dataC['pyComEmpid'], $data[$dataC['pyComEmpid']]['PF'], $data[$dataC['pyComEmpid']]['ESIC']);
            }
        }
   //echo "<pre>";
    //print_r($data);
    //exit;
  return $data;
    }

    /**
     * Get compensation sum
     * 
     * @param array $postData
     * @param string $pyComEmpid
     * @return type
     */
    public function getCTCCompensationSum($postData, $pyComEmpid, $pf, $esic)
    {
        $amountTotal = 0;
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder->select('SUM(pc.amount) as amount')
                ->from(PyCompensationDetails::class, 'pc')
                ->where('pc.ouCode = ?1')
                ->andWhere('pc.langCode = ?2')
                ->andWhere('pc.pgCode = ?3')
                ->andWhere('pc.pyComEmpid = ?4')
                ->andWhere('pc.isModified = ?5')
                ->setParameter('1', $postData['ouCode'])
                ->setParameter('2', $postData['langCode'])
                ->setParameter('3', $postData['payGroup'])
                ->setParameter('4', $pyComEmpid)
                ->setParameter('5', 0);

        $compData = $queryBuilder->getQuery()->getOneOrNullResult();
        $amount = isset($compData['amount']) ? $compData['amount'] : 0;
        $amountTotal = $amount + $pf + $esic;
        return $amountTotal;
    }
}

这是PayrollspendManagerFactory.php

<?php

namespace Dashboard\Service\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Dashboard\Service\PayrollspendManager;
class PayrollspendManagerFactory implements FactoryInterface
{

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $entityManager = $container->get('doctrine.entitymanager.orm_default');

        // Instantiate the service and inject dependencies
        return new PayrollspendManager($entityManager);
    }

}

这是 PayrollspendForm

<?php

namespace Dashboard\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
//use Application\Entity\TmTask;


/**
 * This form is used to collect post data.
 */
class PayrollspendForm extends Form
{
    public $session;
    public $entityManager;
    public $ouCode;
    public $langCode;

 public function __construct()
    {
        // Define form name
        parent::__construct('payrollspend-form');

        // Set POST method for this form
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');
        $this->setAttribute('id', 'payrollspend-form');
        //$this->edit = $edit;
       // $this->ouCode = $session->ouCode;
      //  $this->langCode = $session->langCode;
       // $this->entityManager = $entityManager;
        $this->addElements();
        $this->addInputFilter();
    }
     protected function addElements()
    {
      $this->add([
            'type'  => 'select',
            'name' => 'payPeriod',
            'attributes' => [
                'id' => 'payPeriod',
                'class'=>'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name' => 'payCalender',
            'attributes' => [
                'id' => 'payCalender',
                'class'=>'form-control',
                'disabled' => 'disabled'
            ]
        ]);
          $this->add([
            'type'  => 'select',
            'name' => 'companyCode',
            'attributes' => [
                'id' => 'companyCode',
                'class'=>'form-control'
            ]
        ]);


        $this->add([
            'type'  => 'select',
            'name' => 'payGroup',
            'attributes' => [
                'id' => 'payGroup',
                'class'=>'form-control'
            ]
        ]);
         $this->add([
            'type' => 'text',
            'name' => 'startDate',
            'attributes' => [
                'id' => 'startDate',
                'class' => 'form-control dpd1',
                'data-date-format' => "dd-mm-yyyy"
            ]
        ]);
        $this->add([
            'type' => 'text',
            'name' => 'endDate',
            'attributes' => [
                'id' => 'endDate',
                'class' => 'form-control dpd1',
                'data-date-format' => "dd-mm-yyyy"
            ]
        ]);
         $this->add([           
            'type'  => 'select',
            'name' => 'groupby',
            'attributes' => [
                'id' => 'groupby',
                'class'=>'form-control', 
                'placeholder'=>''
            ],
        ]);

         $this->add([
            'type' => 'submit',
            'name' => 'submit',
            'attributes' => [
                'value' => 'Submit',
                'id' => 'submitbutton',
                'class' => 'btn btn-primary'
            ],
        ]);
}
 private function addInputFilter()
    {
        $inputFilter = new InputFilter();
        $this->setInputFilter($inputFilter);
    }
    public function buildPayGroupData($payGroup) 
    {

        $payGroupData = [];

        foreach($payGroup as $data){
            $payGroupData[$data->pgCode] = $data->pgSdesc;
        }

        $this->get('payGroup')->setEmptyOption('select')->setValueOptions($payGroupData);
    }
      public function buildCompanyData($companys) 
    {
        $companyData = ['' => 'Select'];
        foreach($companys as $data){
            $companyData[$data->companyCode] = $data->companyName;
        }

        $this->get('companyCode')->setValueOptions($companyData);
    }
    public function defaultDate()
    {
        $this->get('startDate')->setValue(date('d-m-Y'));
    }
}

假设 PayrollspendController::addAction() 中的调用是问题所在。

$compData = $this->payrollspendManager->hello($postData);

这是因为$this->payrollspendManager变量还没有定义。

您可以在PayrollspendControllerFactory::__invoke中看到您已经从依赖注入容器中请求了服务;但不要将其注入 PayrollspendController.

的构造函数中
 // PayrollspendControllerFactory::__invoke()
 $entityManager = $container->get('doctrine.entitymanager.orm_default');
 $payrollspendManager = $container->get(PayrollspendManager::class);

 return new PayrollspendController($entityManager);

您应该更新工厂:

 return new PayrollspendController($entityManager, $payrollspendManager);

还有控制器的构造函数允许新的参数(当你在那里时你也可以在预期的界面上键入提示)

use \Doctrine\ORM\EntityManager;
use \Dashboard\Service\PayrollspendManager;

class PayrollspendController  extends AbstractActionController
{
   // ...
   private $entityManager;

   private $payrollspendManager;

   public function __construct(
       EntityManager $entityManager, 
       PayrollspendManager $payrollspendManager
   ){
       $this->entityManager = $entityManager;
       $this->payrollspendManager = $payrollspendManager;
   }
   // ...
}