首页 > 解决方案 > zendframework 3中未定义的变量公司

问题描述

未定义的可变公司

未定义的变量我得到错误

    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中定义一个变量我想解决这个问题

标签: zend-framework3

解决方案


问题是您$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,
    ];
}

推荐阅读