zendframework 3中未定义的变量公司

时间:2019-01-24 11:37:08

标签: zend-framework3

未定义变量公司

我遇到错误时未定义的变量

    public function addPolicyAction() 
    {
        if ($this->sessionContainer->empId == "") 
            {
            return $this->redirect()->toRoute('admin_user_login');
            }
        $ouCode = $this->sessionContainer->ouCode;
        $langCode = $this->sessionContainer->langCode;
        $empId = $this->sessionContainer->empId;
 $arrLabel = array('company_policy','pdid','pdname','file_name','active');
        $commonTransalationLabel = $this->commonTranslation->getCommonTransactionInformation($arrLabel, $langCode);
        $companyPolicyForm = new CompanyPolicyForm($commonTransalationLabel);
         if ($this->getRequest()->isPost()) {
            //  $data = $this->params()->fromPost();
            $request = $this->getRequest();
            $data = array_merge_recursive(
                    $request->getPost()->toArray(), $request->getFiles()->toArray()
            );
            $data['ouCode'] = $ouCode;
            $data['langCode'] = $langCode;
            $companyPolicyForm->setData($data);

            $chkValidate = $this->hrCompanypolicy->findBy([
                'ouCode' => $this->sessionContainer->ouCode,
                'langCode' => $this->sessionContainer->langCode
            ]);

            if ($companyPolicyForm->isValid()) {
               $data = $companyPolicyForm->getData();

               if(isset($_POST['Submit'])){
               $name = $_FILES['fileName']['name'];
               $target_dir = 'public/media/policy_photos/';
               $target_file = $target_dir . basename($_FILES["fileName"]["name"]);
               $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
               $extensions_arr = array("jpg","jpeg","png","gif");
               if( in_array($imageFileType,$extensions_arr) ){
              move_uploaded_file($_FILES['fileName']['tmp_name'],$target_dir.$name);
              }
               }
$company = $this->companyPolicyManager->add($data,$ouCode, $langCode,$empId);
$cpData = $this->companyPolicyManager->getcpDataBycpId($data,$ouCode,$langCode);
$companyPolicyForm->buildCompanyPolicyData($cpData);
$this->flashMessenger()->addMessage($commonTransalationLabel['success_message']);

            } 
           }

        return new ViewModel([
            'form' => $company,
            'companypolicydata' =>  $cpData, 
            'label' => $commonTransalationLabel,
            'form' => $companyPolicyForm,
            'flashMessages' => $this->flashMessenger()->getMessages()
        ]);
    }

我想在zendframework 3中删除未定义的变量

我使用zendframework 3并在zendframework 3中获取未定义的变量,代码中有什么问题?

我想在zendframework 3中定义变量的方法

1 个答案:

答案 0 :(得分:0)

问题是您在$company语句中使用了return new ViewModel变量,但是仅在整个表单有效时才创建变量。

请确保您通过工厂为控制器提供了Form实例(以您需要的任何实例,例如CompanyForm),而不是在做什么。然后按照下面的代码进行操作(我已经删除了一些错误检查):

public function editAction()
{
    $id = $this->params()->fromRoute('id', null);

    /** @var Company $entity */
    $entity = $this->getObjectManager()->getRepository(Company::class)->find($id);

    /** @var CompanyForm $form */
    $form = $this->getForm();
    $form->bind($entity);

    /** @var Request $request */
    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            /** @var Company $entity */
            $entity = $form->getObject();

            $this->getObjectManager()->persist($entity);

            try {
                $this->getObjectManager()->flush();
            } catch (Exception $e) {

                throw new Exception('Could not save. Error was thrown, details: ', $e->getMessage());
            }

            return $this->redirectToRoute('companies/view', ['id' => $entity->getId()]);
        }
    }

    return [
        'form' => $form,
    ];
}