Symfony2.7在树枝中显示combox项目

时间:2015-11-20 05:10:54

标签: symfony doctrine-orm

我有一张名为公司和部门的表。公司与部门有一对多的关系。我创建了两个实体并在两者中指定了关系。请查看两个实体

Department.php

<?php

namespace Benerite\CompanyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Department
 *
 * @ORM\Table("departments")
 * @ORM\Entity(repositoryClass="Benerite\CompanyBundle\Entity\DepartmentRepository")
 */
class Department
{
    /**
     * @ORM\ManyToOne(targetEntity="Company", inversedBy="departments")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     */
    protected $company;

    /**
     * @var employeeJobInfo
     * 
     * @ORM\OneToMany(targetEntity="Benerite\EmployeeBundle\Entity\EmployeeJobInfo", 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(\Benerite\EmployeeBundle\Entity\EmployeeJobInfo $employeeJobInfo) {
        $this->employeeJobInfo = $employeeJobInfo;
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * @var string
     *
     * @ORM\Column(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 Benerite\CompanyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Company
 *
 * @ORM\Table("companies")
 * @ORM\Entity(repositoryClass="Benerite\CompanyBundle\Entity\CompanyRepository")
 */
class Company
{
    /**
     * @var departments
     * @ORM\OneToMany(targetEntity="Department", mappedBy="company")
     */
    protected $departments;

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

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

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

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

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

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

    /**
     * @var subscriptionDetails
     * 
     * @ORM\OneToMany(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(\Benerite\EmployeeBundle\Entity\EmployeeEmploymentStatus $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
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

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

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(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();
    }
}

我已经使用this和它的正确创建了模式。我已经为两个实体生成了crud表单,它也给了我正确的表单。问题是,在我的部门创建和编辑页面时,它在渲染时给我一个这样的组合框

<select id="benerite_companybundle_department_company" name="benerite_companybundle_department[company]">
  <option value=""></option>            
  <option value="1">1</option>            
  <option value="2">2</option>
</select>

这不是我想要的,应该像

<select id="benerite_companybundle_department_company" name="benerite_companybundle_department[company]">
      <option value="">please select</option>            
      <option value="1">comapny 1</option>            
      <option value="2">comapny 2</option>
    </select>

这是我的new.twig.html文件

{% extends '::base.html.twig' %}

{% block body -%}
    <h1>Department creation</h1>
    {{ form_start(form) }}

        <div>
            {{ form_label(form.company) }}
            {{ form_widget(form.company) }}

        </div>
        <div>
            {{ form_label(form.departmentStatus) }}
            {{ form_widget(form.departmentStatus) }}
        </div>

        <div>
            {{ form_label(form.departmentName) }}
            {{ form_widget(form.departmentName) }}

        </div>

    {{ form_end(form) }}

    <ul class="record_actions">
        <li>
            <a href="{{ path('department') }}">
                Back to the list
            </a>
        </li>
    </ul>
{% endblock %}

我使用symfony2.7和mysql作为数据库。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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

我希望这会按你的意愿运作。

或者,如果您想编写自定义查询,请查看以下网址: http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities