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
472 views
in Technique[技术] by (71.8m points)

symfony - Symfony2.7 displaying combox items in twig

I have a table named company and departments. Company having one to many relationship with departments. I have created both the entities and specified the relationships in both. Please take a look at both the entities

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 integer
     */
    public function getCompanyId()
    {
        return $this->companyId;
    }

    /**
     * Set departmentName
     *
     * @param string $departmentName
     *
     * @return Department
     */
    public function setDepartmentName($departmentName)
    {
        $this->departmentName = $departmentName;

        return $this;
    }

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

    /**
     * Set departmentStatus
     *
     * @param string $departmentStatus
     *
     * @return Department
     */
    public function setDepartmentStatus($departmentStatus)
    {
        $this->departmentStatus = $departmentStatus;

        return $this;
    }

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


}

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;

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

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

    /**
     * @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)
    {
 

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

1 Reply

0 votes
by (71.8m points)

You can do like below:

public function buildForm(FormBuilderInterface $builder, array $options)
{
      $builder
          ->add('company', 'entity',  array(
              'class' => 'BeneriteCompanyBundle:Company',
              'choice_label' => 'companyName'
          )  
          ->add('departmentName')
          ->add('departmentStatus')
      ;
}

I hope this will work as you want.

Or if you want to write custom query go through below url: http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities


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

...