Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

php - Display specific fields from database in value and text fields of a combobox in Symfony2

I have a table named company which is having one to many relationship with a department table. I have created both the entities using Doctrine generator by specifying the relationships. I also have generated the schema and everything went fine

Please take a look at both my entities

Company.php

<?php

namespace BeneriteCompanyBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * Company
 *
 * @ORMTable("companies")
 * @ORMEntity(repositoryClass="BeneriteCompanyBundleEntityCompanyRepository")
 */
class Company
{
    /**
     * @var departments
     * @ORMOneToMany(targetEntity="Department", mappedBy="company")
     */
    protected $departments;

    /**
     * @var divisions
     * @ORMOneToMany(targetEntity="Division", mappedBy="company")
     */
    protected $divisions;

    /**
     * @var employmentStatuses
     * @ORMOneToMany(targetEntity="EmploymentStatus", mappedBy="company")
     */
    protected $employmentStatuses;

    /**
     * @var jobTitles
     * @ORMOneToMany(targetEntity="JobTitle", mappedBy="company")
     */
    protected $jobTitles;

    /**
     * @var companyLocations
     * @ORMOneToMany(targetEntity="Location", mappedBy="company")
     */
    protected $companyLocations;

    /**
     * @var remunerationChangeReasons
     * @ORMOneToMany(targetEntity="RemunerationChangeReason", mappedBy="company")
     */
    protected $remunerationChangeReasons;

    /**
     * @var roles
     * @ORMOneToMany(targetEntity="Role", mappedBy="company")
     */
    protected $roles;

    /**
     * @var subscriptionDetails
     * 
     * @ORMOneToMany(targetEntity="SubscriptionDetail", mappedBy="company")
     */
    protected $subscriptionDetails;

    /**
     * @var employeeBasicInfo
     * 
     * @ORMOneToMany(targetEntity="BeneriteEmployeeBundleEntityEmployeeBasicInfo", mappedBy="companies")
     */
    protected $employeeBasicInfo;

    public function __construct() {
        $this->departments = new ArrayCollection();
        $this->divisions = new ArrayCollection();
        $this->employmentStatuses = new ArrayCollection();
        $this->jobTitles = new ArrayCollection();
        $this->companyLocations = new ArrayCollection();
        $this->remunerationChangeReasons = new ArrayCollection();
        $this->roles = new ArrayCollection();
        $this->subscriptionDetails = new ArrayCollection();
        $this->employeeBasicInfo = new ArrayCollection();
    }

    function getDepartments() {
        return $this->departments;
    }

    function getDivisions() {
        return $this->divisions;
    }

    function getEmploymentStatuses() {
        return $this->employmentStatuses;
    }

    function getJobTitles() {
        return $this->jobTitles;
    }

    function getCompanyLocations() {
        return $this->companyLocations;
    }

    function getRemunerationChangeReasons() {
        return $this->remunerationChangeReasons;
    }

    function getRoles() {
        return $this->roles;
    }

    function getSubscriptionDetails() {
        return $this->subscriptionDetails;
    }

    function setDepartments(Department $departments) {
        $this->departments = $departments;
    }

    function setDivisions(Division $divisions) {
        $this->divisions = $divisions;
    }

    function setEmploymentStatuses(BeneriteEmployeeBundleEntityEmployeeEmploymentStatus $employmentStatuses) {
        $this->employmentStatuses = $employmentStatuses;
    }

    function setJobTitles(JobTitle $jobTitles) {
        $this->jobTitles = $jobTitles;
    }

    function setCompanyLocations(Location $companyLocations) {
        $this->companyLocations = $companyLocations;
    }

    function setRemunerationChangeReasons(RemunerationChangeReason $remunerationChangeReasons) {
        $this->remunerationChangeReasons = $remunerationChangeReasons;
    }

    function setRoles(Role $roles) {
        $this->roles = $roles;
    }

    function setSubscriptionDetails(SubscriptionDetail $subscriptionDetails) {
        $this->subscriptionDetails = $subscriptionDetails;
    }

    function getEmployeeBasicInfo() {
        return $this->employeeBasicInfo;
    }

    function setEmployeeBasicInfo(BeneriteEmployeeBundleEntityEmployeeBasicInfo $employeeBasicInfo) {
        $this->employeeBasicInfo = $employeeBasicInfo;
    }

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="company_name", type="string", length=255)
     */
    private $companyName;

    /**
     * @var string
     *
     * @ORMColumn(name="company_reg_code", type="string", length=255)
     */
    private $companyRegCode;

    /**
     * @var string
     *
     * @ORMColumn(name="account_owner", type="string", length=255)
     */
    private $accountOwner;

    /**
     * @var string
     *
     * @ORMColumn(name="account_email", type="string", length=255)
     */
    private $accountEmail;

    /**
     * @var string
     *
     * @ORMColumn(name="company_url", type="string", length=255)
     */
    private $companyUrl;

    /**
     * @var string
     *
     * @ORMColumn(name="company_status", type="string", length=255)
     */
    private $companyStatus;

    /**
     * @var DateTime
     *
     * @ORMColumn(name="created_date", type="datetime")
     */
    private $createdDate;

    /**
     * @var DateTime
     *
     * @ORMColumn(name="last_updated_date", type="datetime")
     */
    private $lastUpdatedDate;


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

    /**
     * Set companyName
     *
     * @param string $companyName
     *
     * @return Company
     */
    public function setCompanyName($companyName)
    {
        $this->companyName = $companyName;

        return $this;
    }

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

    /**
     * Set companyRegCode
     *
     * @param string $companyRegCode
     *
     * @return Company
     */
    public function setCompanyRegCode($companyRegCode)
    {
        $this->companyRegCode = $companyRegCode;

        return $this;
    }

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

    /**
     * Set accountOwner
     *
     * @param string $accountOwner
     *
     * @return Company
     */
    public function setAccountOwner($accountOwner)
    {
        $this->accountOwner = $accountOwner;

        return $this;
    }

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

    /**
     * Set accountEmail
     *
     * @param string $accountEmail
     *
     * @return Company
     */
    public function setAccountEmail($accountEmail)
    {
        $this->accountEmail = $accountEmail;

        return $this;
    }

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

    /**
     * Set companyUrl
     *
     * @param string $companyUrl
     *
     * @return Company
     */
    public function setCompanyUrl($companyUrl)
    {
        $this->companyUrl = $companyUrl;

        return $this;
    }

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

    /**
     * Set companyStatus
     *
     * @param string $companyStatus
     *
     * @return Company
     */
    public function setCompanyStatus($companyStatus)
    {
        $this->companyStatus = $companyStatus;

        return $this;
    }

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

    /**
     * Set createdDate
     *
     * @param DateTime $createdDate
     *
     * @return Company
     */
    public function setCreatedDate($createdDate)
    {
        $this->createdDate = $createdDate;

        return $this;
    }

    /**
     * Get createdDate
     *
     * @return DateTime
     */
    public function getCreatedDate()
    {
        return $this->createdDate;
    }

    /**
     * Set lastUpdatedDate
     *
     * @param DateTime $lastUpdatedDate
     *
     * @return Company
     */
    public function setLastUpdatedDate($lastUpdatedDate)
    {
        $this->lastUpdatedDate = $lastUpdatedDate;

        return $this;
    }

    /**
     * Get lastUpdatedDate
     *
     * @return DateTime
     */
    public function getLastUpdatedDate()
    {
        return $this->lastUpdatedDate;
    }

    public function __toString()
    {
        return (string)$this->getId();
    }
}

Department.php

<?php

namespace BeneriteCompanyBundleEntity;

use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;

/**
 * Department
 *
 * @ORMTable("departments")
 * @ORMEntity(repositoryClass="BeneriteCompanyBundleEntityDepartmentRepository")
 */
class Department
{
    /**
     * @ORMManyToOne(targetEntity="Company", inversedBy="departments")
     * @ORMJoinColumn(name="company_id", referencedColumnName="id")
     */
    protected $company;

    /**
     * @var employeeJobInfo
     * 
     * @ORMOneToMany(targetEntity="BeneriteEmployeeBundleEntityEmployeeJobInfo", mappedBy="department")
     */
    protected $employeeJobInfo;

    public function __construct()
    {
        $this->employeeJobInfo = new ArrayCollection();
    }

    function getCompany() {
        return $this->company;
    }

    function getEmployeeJobInfo() {
        return $this->employeeJobInfo;
    }

    function setCompany(Company $company) {
        $this->company = $company;
    }

    function setEmployeeJobInfo(BeneriteEmployeeBundleEntityEmployeeJobInfo $employeeJobInfo) {
        $this->employeeJobInfo = $employeeJobInfo;
    }

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORMColumn(name="company_id", type="integer" , nullable = false)
     */
    private $companyId;

    /**
     * @var string
     *
     * @ORMColumn(name="department_name", type="string", length=255)
     */
    private $departmentName;

    /**
     * @var string
     *
     * @ORMColumn(name="department_status", type="string", length=255)
     */
    private $departmentStatus;


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

    /**
     * Set companyId
     *
     * @param integer $companyId
     *
     * @return Department
     */
    public function setCompanyId($companyId)
    {
        $this->companyId = $companyId;

        return $this;
    }

    /**
     * Get companyId
     *
     * @return 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To display company name instead of its id, use the property option (for Symfony 2.6 and bellow) or the choice_label option (for Symfony 2.7+) :

For Symfony 2.6 and bellow :

<?php

namespace BeneriteCompanyBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;

class DepartmentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('company', 'entity', array(
                'class' => 'CompanyBundle:Company',
                'property' => 'companyName',
            ))  
            ->add('departmentName')
            ->add('departmentStatus')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BeneriteCompanyBundleEntityDepartment'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'benerite_companybundle_department';
    }
}

For Symfony 2.7 and next :

<?php

namespace BeneriteCompanyBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;

class DepartmentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('company', 'entity', array(
                'class' => 'CompanyBundle:Company',
                'choice_label' => 'companyName',
            ))  
            ->add('departmentName')
            ->add('departmentStatus')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BeneriteCompanyBundleEntityDepartment'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'benerite_companybundle_department';
    }
}

And by the way, in your Twig instead of

{{ form_label(form.company) }}
{{ form_widget(form.company) }}

you can write form_row(form.company).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...