This is CompanyPolicycontroller.php
<?php
namespace WfmHrOrganisationStructure\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Session\SessionManager;
use Zend\Validator\File\Size;
use WfmHrOrganisationStructure\Form\CompanyPolicyForm;
use Application\Entity\HrCompanypolicy;
class CompanyPolicyController extends AbstractActionController {
private $companyPolicyManager;
private $entityManager;
private $sessionContainer;
private $hrCompanypolicy;
public function __construct($entityManager, $companyPolicyManager, $commonTranslation) {
$this->entityManager = $entityManager;
$this->companyPolicyManager = $companyPolicyManager;
$this->hrCompanypolicy = $this->entityManager->getRepository(HrCompanypolicy::class);
$sessionManager = new SessionManager();
$this->commonTranslation = $commonTranslation;
$this->sessionContainer = new Container('ContainerNamespace', $sessionManager);
}
public function addPolicyAction()
{
if ($this->sessionContainer->empId == "")
{
return $this->redirect()->toRoute('admin_user_login');
}
$ouCode = $this->sessionContainer->ouCode;
$langCode = $this->sessionContainer->langCode;
// $arrLabel = array('cost_center', 'code', 'short_desc', 'long_desc', 'active', 'notes', 'success_message', 'duplicate_code_error_message', 'no_record_found', 'err_code', 'err_valid_code', 'err_short_desc', 'err_valid_short_desc');
$arrLabel = array('company_policy','pdid','pdname','file_name');
$commonTransalationLabel = $this->commonTranslation->getCommonTransactionInformation($arrLabel, $langCode);
$companyPolicyForm = new CompanyPolicyForm($commonTransalationLabel);
//$cpData = $this->companyPolicyManager->getcpDataBycpId($data,$ouCode,$langCode);
//$companyPolicyForm->buildCompanyPolicyData($cpData);
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);
/*
* added for duplicate validation
*/
$chkValidate = $this->hrCompanypolicy->findBy([
'ouCode' => $this->sessionContainer->ouCode,
'langCode' => $this->sessionContainer->langCode
]);
// $data = $this->params()->fromPost();
if ($companyPolicyForm->isValid()) {
$data = $companyPolicyForm->getData();
$uploadDir = './public/media/company_photos/';
if (!empty($data['$fileName'])) {
$fileName = rand() . '_' . $data['$fileName']['name'];
move_uploaded_file($data['$fileName']["tmp_name"], $uploadDir . $fileName);
$data['$fileName']['name'] = $fileName;
}
$company = $this->companyPolicyManager->add($data,$ouCode, $langCode);
//$companyPolicy = $this->companyPolicyManager->getcpDataBycpId($data,$ouCode,$langCode);
$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']);
}
}
//echo '<pre>';
//print_r($cpData);
//exit;
//echo $errorMessage;
// echo 'hi'; exit;
return new ViewModel([
'form' => $company,
'companypolicydata' => $cpData,
'label' => $commonTransalationLabel,
'form' => $companyPolicyForm,
'flashMessages' => $this->flashMessenger()->getMessages()
]);
}
This is my entity
/**
* @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;
}
This is my CompanyPolicyManager code
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
public function add($data,$ouCode,$langCode) {
$companyPolicyEntity = new HrCompanyPolicy();
$companyPolicyEntity->ouCode = $ouCode;
//echo '<pre>';
//print_r($data['ouCode']);
//echo '</pre>';
//exit();
if ($data['fileName']['name'] != "") {
$companyPolicyEntity->fileName = $data['fileName']['name'];
}
$companyPolicyEntity->langCode = $langCode;
$companyPolicyEntity->pdId = $data['pdid'];
$companyPolicyEntity->pdName = $data['pdname'];
$companyPolicyEntity->fileName = $data['fileName'];
$companyPolicyEntity->createdDate = new \DateTime();
$companyPolicyEntity->createdBy = 4342;
$companyPolicyEntity->modifiedDate = new \DateTime();
$companyPolicyEntity->modifiedBy = 45345;
$this->entityManager->persist($companyPolicyEntity);
$this->entityManager->flush();
//echo $companyPolicyEntity->getId();exit;
}
public function getcpDataBycpId($data,$ouCode,$langCode)
{
//echo '<pre>';
//echo 'in';
// exit;
//$ouCode = $session->ouCode;
//$langCode = $session->langCode;
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select("cp.pdId,cp.pdName,cp.fileName")
->from(HrCompanypolicy::class, 'cp')
->where('cp.ouCode = ?1')
->andWhere('cp.langCode = ?2')
->setParameter('1', $ouCode)
->setParameter('2', $langCode);
$cpData = $queryBuilder->getQuery()->getResult();
return $cpData;
}
This is companypolicyform.php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace WfmHrOrganisationStructure\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Input;
class CompanyPolicyForm extends Form
{
public $label;
public function __construct($commonTransalationLabel)
{
//echo 'hi';exit;
parent::__construct('company-policy-form');
// Set POST method for this form
$this->setAttribute('method', 'post');
$this->label = $arraylabel;
//$this->setAttribute('target', '_blank');
$this->setAttribute('enctype', 'multipart/form-data');
$this->label = $commonTransalationLabel;
$this->setAttribute('name','companypolicylist');
$this->addElements();
$this->addInputFilter();
}
public function addElements()
{
$this->add([
'type' => 'text',
'name' => 'code',
'attributes' => [
'id' => 'code',
'class' => 'form-control ',
'style' => 'width:auto',
'placeholder' => '',
'maxlength' => 10,
],
]);
$this->add([
'type' => 'text',
'name' => 'pdid',
'attributes' => [
'id' => 'pdid',
'class' => 'form-control ',
'style' => 'width:auto',
'placeholder' => '',
'maxlength' => 10,
],
]);
$this->add([
'type' => 'text',
'name' => 'pdname',
'attributes' => [
'id' => 'pdname',
'class' => 'form-control',
'style' => 'width:auto',
'placeholder' => '',
'maxlength' => 15
],
]);
$this->add([
'type' => 'file',
'name' => 'fileName',
'attributes' => [
'id' => 'fileName',
// 'class' => 'form-control',
'placeholder' => ''
]
]);
/*$this->add([
'type' => 'file',
'name' => 'pdfilename',
'attributes' => [
'id' => 'filename',
'class' => 'form-control ',
//'style' => 'width:auto',
'placeholder' => '',
'maxlength' => 40
],
]);*/
$this->add([
'name' => 'Submit',
'attributes' => [
'type' => 'submit',
'value' => 'Submit',
'id' => 'submitButton',
'class' => 'btn btn-primary btn-margin'
],
]);
}
This is add-policy.phtml
<div class="form-group margin1">
<label class="col-md-2" for=""><label for="">Policy File Name</label><sup class="required"></sup></label>
<div class="col-md-3">
<?= $this->formElement($form->get('fileName')); ?>
<!--<span style="color:red"><?= $this->formElementErrors($form->get('fileName')); ?></span>-->
<img src="<?php echo '../../../../../media/company_photos/' . $photoUrl; ?>" width="50px">
</div>
<div class="clearfix"></div>
</div>
I'm trying to upload the image or pdf or doc in database table as file_name column but it is inserted as Array in table column.
how to insert the image or pdf or doc in table.
why it is going as Array in value....how to solve it
The image I have uploaded is going in form of Array type simple in columns Array
if need any more file will help you
Comments
Post a Comment