如何使用奏鸣曲管理包在配置文件编辑时上传图像和显示

时间:2013-11-28 12:27:48

标签: php sonata-admin symfony-2.3

我正在使用sonata管理包并创建一个员工表单以保存员工详细信息,我想上传员工照片并在点击编辑个人资料时显示该照片,因为我正在编写以下代码: 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')
        ;
    }
}

我的实体类文件位置和代码是 - E:\ XAMPP \ htdocs中\ SymfonyStart \ SRC \西格玛\ EmployeeBundle \实体

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

通过这个我能够上传图像,但它保存在数据库中,如“xampp / tmp / php6767.xt”。如何正确保存此图像并在编辑配置文件中显示?我按照教程文档进行了操作,但我根本不知道在哪里以及如何根据我的代码放置代码。

1 个答案:

答案 0 :(得分:1)

您应该将SonataMediaAdmin与SonataAdminBundle一起使用,以便存储您的员工个人资料照片。

使用它,您可以在EmployeeAdmin类(在configureFormFields()方法中)写下类似的东西:

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

您还应该在SonataMediaBundle配置中创建一个特定的上下文(在我之前的代码块中名为employee_pictures)来存储它们。