undefined variable company in zendframework 3

試著忘記壹切 提交于 2020-01-06 08:56:05

问题


undefined variable company

undefined variable i m getting error

    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()
        ]);
    }

i want to remove undefined variable in zendframework 3

i m using zendframework 3 and getting undefined variable in zendframework 3 what is the issue in the code ?

How to defined a variable in zendframework 3 i want to solve the issue


回答1:


Problem is that you're using the $company variable in your return new ViewModel statement, but you only create the variable when the entire form is valid.

Instead of what you're doing, make sure that you provide a Form instance (whichever you need, e.g. CompanyForm) to your controller via the Factory. Then have your function along the lines like below (I've removed some error checking):

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,
    ];
}


来源:https://stackoverflow.com/questions/54345741/undefined-variable-company-in-zendframework-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!