zendframework 3中未定义的变量公司 [英] undefined variable company in zendframework 3

查看:102
本文介绍了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中删除未定义的变量

i want to remove undefined variable in zendframework 3

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

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

如何在zendframework 3中定义一个变量,我想解决这个问题

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

推荐答案

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

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.

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

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

这篇关于zendframework 3中未定义的变量公司的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆