Hina Hina - 6 months ago 140
PHP Question

How to upload image and display on edit of profile using sonata admin bundle

I am using sonata admin bundle and creating one employee form to save employee detail and I want to upload employee photo and display that photo on click of edit profile for that I am writing following code in location:

E:\xampp\htdocs\SymfonyStart\src\Sigma\EmployeeBundle\Admin\EmployeeAdmin


// src/Sigma/EmployeeBundle/Admin/PostAdmin.php

namespace Sigma\EmployeeBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class EmployeeAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper

->add('name', 'text', array('label' => 'Name'))
->add('designation', 'choice', array(
'choices' => array('pm' => 'PM', 'pl' => 'PL','pm' => 'PM', 'pc' => 'PC','pm' => 'PM', 'tl' => 'TL','sse' => 'SSE', 'se' => 'SE','others' => 'Others'),
'required' => true,

))
->add('lob', 'choice', array(
'choices' => array('ba' => 'BA', 'bi' => 'BI','java' => 'JAVA','qa' => 'QA', 'ob' => 'OB','web' => 'WEB'),
'required' => true,

))
->add('emp_id')
->add('experience', 'text', array('label' => 'Experience'))


->add('skills', 'textarea', array('label' => 'Skills'))
->add('email', 'email', array('label' => 'Email'))
->add('mobile', 'text', array('label' => 'Mobile'))
->add('attachment', 'file', array(
'required' => false,
'data_class' => null
)
);


;
}

// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('designation')
->add('name')
->add('emp_id')
->add('lob')

;
}

// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper

->add('name')
->addIdentifier('designation')
->add('emp_id')
->add('lob')
->add('attachment')
;
}
}


and my entity class file location and code is -
E:\xampp\htdocs\SymfonyStart\src\Sigma\EmployeeBundle\Entity

namespace Sigma\EmployeeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Employee
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Sigma\EmployeeBundle\Entity\EmployeeRepository")
*/
class Employee
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;

/**
* @var string
*
* @ORM\Column(name="lob", type="string", length=255)
*/
private $lob;

/**
* @var string
*
* @ORM\Column(name="experience", type="string", length=255)
*/
private $experience;

/**
* @var string
*
* @ORM\Column(name="skills", type="string", length=255)
*/
private $skills;

/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;

/**
* @var string
*
* @ORM\Column(name="mobile", type="integer")
*/
private $mobile;

/**
* @var string
*
* @ORM\Column(name="emp_id", type="integer")
*/
private $emp_id;

/**
* @var string
*
* @ORM\Column(name="designation", type="string", length=255)
*/
private $designation;


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


/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set designation
*
* @param string $designation
* @return Employee
*/
public function setDesignation($designation)
{
$this->designation = $designation;

return $this;
}

/**
* Get designation
*
* @return string
*/
public function getDesignation()
{
return $this->designation;
}

/**
* Set first_name
*
* @param string $firstName
* @return Employee
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set lob
*
* @param string $lob
* @return Employee
*/
public function setLob($lob)
{
$this->lob = $lob;

return $this;
}

/**
* Get lob
*
* @return string
*/
public function getLob()
{
return $this->lob;
}


/**
* Set experience
*
* @param string $experience
* @return Employee
*/
public function setExperience($experience)
{
$this->experience = $experience;

return $this;
}

/**
* Get experience
*
* @return string
*/
public function getexperience()
{
return $this->experience;
}

/**
* Set skills
*
* @param string $skills
* @return Employee
*/
public function setSkills($skills)
{
$this->skills = $skills;

return $this;
}

/**
* Get skills
*
* @return string
*/
public function getSkills()
{
return $this->skills;
}

/**
* Set email
*
* @param string $email
* @return Employee
*/
public function setEmail($email)
{
$this->email = $email;

return $this;
}

/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* Set mobile
*
* @param integer $mobile
* @return Employee
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;

return $this;
}

/**
* Get mobile
*
* @return integer
*/
public function getMobile()
{
return $this->mobile;
}

/**
* Set attachment
*
* @param string $attachment
* @return Employee
*/
public function setAttachment($attachment)
{
$this->attachment = $attachment;

return $this;
}

/**
* Get attachment
*
* @return string
*/
public function getAttachment()
{
return $this->attachment;
}
/**
* Set emp_id
*
* @param integer $empId
* @return Employee
*/
public function setEmpId($empId)
{
$this->emp_id = $empId;

return $this;
}

/**
* Get emp_id
*
* @return integer
*/
public function getEmpId()
{
return $this->emp_id;
}
}


by this I am able to upload image but it saving in database like - "xampp/tmp/php6767.xt". How I can save this image properly and and display on edit profile? I followed the tutorial documentation but exactly I am not getting where and how to place code according to my code.

Answer

You should use SonataMediaAdmin with the SonataAdminBundle in order to store your employee profile pictures.

Using it, you could write in your EmployeeAdmin class (in the configureFormFields() method) something like that:

$formMapper->add('attachment', 'sonata_media_type', array(
    'provider' => 'sonata.media.provider.image',
    'context'  => 'employee_pictures',
));

You should also create a specific context (named employee_pictures in my previous code block) in the SonataMediaBundle configuration to store them.

Comments