类型为Application \ Entity \ HrCompanyPolicy的实体缺少为字段“ ouCode”分配的ID

时间:2019-02-06 10:41:14

标签: php doctrine

  

类型为Application \ Entity \ HrCompanyPolicy的实体缺少为字段“ ouCode”分配的ID。该实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果要自动生成标识符,则需要相应地调整元数据映射。

<?php
namespace WfmHrOrganisationStructure\Service;
use Application\Entity\HrCompanyPolicy;
class CompanyPolicyManager 
{
    private $entityManager;
    const activeField = 1;
    public function __construct($entityManager)
    {
    $this->entityManager = $entityManager;
    }
    public function add($data,$ouCode,$langCode,$empId) {

        $companyPolicyEntity = new HrCompanyPolicy();
        $companyPolicyEntity->ouCode = $data['ouCode'];
        $companyPolicyEntity->langCode = $data['langCode'];
        $companyPolicyEntity->pdId = $data['pdid'];
        $companyPolicyEntity->pdName = $data['pdname'];
        $companyPolicyEntity->fileName = $data['fileName']['name'];
        $companyPolicyEntity->locActive = $data['active'];
        $companyPolicyEntity->createdDate = new \DateTime();
        $companyPolicyEntity->createdBy = $empId;
        $companyPolicyEntity->modifiedDate = new \DateTime();
        $companyPolicyEntity->modifiedBy = $empId;
        $this->entityManager->persist($companyPolicyEntity);
        $this->entityManager->flush();
        }

This is HrCompanyPolicy.php entity 
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * HrCompanyPolicy
 *
 * @ORM\Table(name="hr_company_policy")
 * @ORM\Entity
 */
class HrCompanyPolicy
{
    /**
     * @var integer
     *
     * @ORM\Column(name="pd_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    public $pdId;

    /**
     * @var integer
     *
     * @ORM\Column(name="ou_code", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    public $ouCode;

    /**
     * @var string
     *
     * @ORM\Column(name="lang_code", type="string", length=3, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    public $langCode;

    /**
     * @var string
     *
     * @ORM\Column(name="pd_name", type="string", length=255, nullable=false)
     */
    public $pdName;

    /**
     * @var string
     *
     * @ORM\Column(name="file_name", type="string", length=255, nullable=false)
     */
    public $fileName;

    /**
     * @var string
     *
     * @ORM\Column(name="created_by", type="string", length=10, nullable=false)
     */
    public $createdBy;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_date", type="datetime", nullable=false)
     */
    public $createdDate;

    /**
     * @var string
     *
     * @ORM\Column(name="modified_by", type="string", length=10, nullable=false)
     */
    public $modifiedBy;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="modified_date", type="datetime", nullable=false)
     */
    public $modifiedDate;
    /**
     * @var integer
     *
     * @ORM\Column(name="loc_active", type="integer", nullable=true)
     */
    public $locActive = '1';
}

This is manager code 
<?php
namespace WfmHrOrganisationStructure\Service;
use Application\Entity\HrCompanyPolicy;
class CompanyPolicyManager 
{
    private $entityManager;
    const activeField = 1;
    public function __construct($entityManager)
    {
    $this->entityManager = $entityManager;
    }
    public function add($data,$ouCode,$langCode,$empId) {

        $companyPolicyEntity = new HrCompanyPolicy();
        $companyPolicyEntity->ouCode = $data['ouCode'];
        $companyPolicyEntity->langCode = $data['langCode'];
        $companyPolicyEntity->pdId = $data['pdid'];
        $companyPolicyEntity->pdName = $data['pdname'];
        $companyPolicyEntity->fileName = $data['fileName']['name'];
        $companyPolicyEntity->locActive = $data['active'];
        $companyPolicyEntity->createdDate = new \DateTime();
        $companyPolicyEntity->createdBy = $empId;
        $companyPolicyEntity->modifiedDate = new \DateTime();
        $companyPolicyEntity->modifiedBy = $empId;
        $this->entityManager->persist($companyPolicyEntity);
        $this->entityManager->flush();
        }
This is my controller 
 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;
        $company = '';
        $cpData ='';



        $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('pdf');
               if( in_array($imageFileType,$extensions_arr) ){
              move_uploaded_file($_FILES['fileName']['tmp_name'],$target_dir.$name);
              }

               }
if(isset($_FILES['fileName']))
 {
    $errors     = array();
    $maxsize    = 3097152;
    $acceptable = array(
        'pdf'
    );
if(($_FILES['fileName']['size'] >= $maxsize) || ($_FILES["fileName"]["size"] == 0)) 
{
        $errors[] = 'File too large. File must be less than 2 megabytes.';
    }
if(!empty($_FILES['fileName']['name']))
{
  $errors[] = 'File is not uploaded';
}
if((!in_array($_FILES['fileName']['type'], $acceptable)) && (!empty($_FILES["fileName"]["type"])))
 {
        $errors[] = 'Invalid file type. Only PDF  types are accepted.';
}
if(count($errors) === 0) 
{

   move_uploaded_file($_FILES['fileName']['tmpname'],$target_file);
}
else
{
foreach($errors as $company) {
            echo ''.$company.'';

        }


    }
}
$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 $this->redirect()->toRoute('wfm_job_company_policy', ['action' => 'add']);
            } 
           }

        return new ViewModel([
            'form' => $company,

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

类型为Application \ Entity \ HrCompanyPolicy的实体缺少为字段“ ouCode”分配的ID。该实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果要自动生成标识符,则需要相应地调整元数据映射。

此处ouCode是表中的主键

0 个答案:

没有答案