问题描述
我有一个名为公司和部门的表。公司与部门有一对多的关系。我已经创建了两个实体,并在两者中指定了关系。请看两个实体Department.php
<?php
命名空间Benerite\CompanyBundle\Entity;
使用Doctrine\ORM\Mapping作为ORM;
使用Doctrine\Common\Collections\ArrayCollection;
/ **
*部门
*
* @ 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;
}
函数setCompany(Company $ company){
$ this-> company = $ company;
}
函数setEmployeeJobInfo(\Benerite\EmployeeBundle\Entity\EmployeeJobInfo $ employeeJobInfo){
$ this-> employeeJobInfo = $ employeeJobInfo;
}
/ **
* @var整数
*
* @ ORM\Column(name =id,type =integer )
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* /
private $ id;
/ **
* @var整数
*
* @ 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;
/ **
*获取id
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}
/ **
*设置companyId
*
* @param整数$ companyId
*
* @return Department
* /
public function setCompanyId($ companyId)
{
$ this-> companyId = $ companyId;
return $ this;
}
/ **
* Get companyId
*
* @return integer
* /
public function getCompanyId )
{
return $ this-> companyId;
}
/ **
*设置departmentName
*
* @param string $ departmentName
*
* @return Department
* /
public function setDepartmentName($ departmentName)
{
$ this-> departmentName = $ departmentName;
return $ this;
}
/ **
*获取departmentName
*
* @return string
* /
public function getDepartmentName )
{
return $ this-> departmentName;
}
/ **
*设置departmentStatus
*
* @param string $ departmentStatus
*
* @return Department
* /
public function setDepartmentStatus($ departmentStatus)
{
$ this-> departmentStatus = $ departmentStatus;
return $ this;
}
/ **
*获取departmentStatus
*
* @return string
* /
public function getDepartmentStatus( )
{
return $ this-> departmentStatus;
}
}
Company.php
<?php
命名空间Benerite\CompanyBundle\Entity;
使用Doctrine\ORM\Mapping作为ORM;
/ **
*公司
*
* @ ORM\Table(公司)
* @ ORM\Entity(repositoryClass = Benerite\CompanyBundle\Entity\CompanyRepository)
* /
class公司
{
/ **
* @var department
* @ ORM\OneToMany(targetEntity =Department,mappedBy =company)
* /
protected $ departments;
/ **
* @var分区
* @ ORM\OneToMany(targetEntity =Division,mappedBy =company)
* /
保护$ 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角色
* @ 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;
}
函数getEmploymentStatuses(){
return $ this-> employmentStatuses;
}
函数getJobTitles(){
return $ this-> jobTitles;
}
function getCompanyLocations(){
return $ this-> companyLocations;
}
函数getRemunerationChangeReasons(){
return $ this-> remunerationChangeReasons;
}
函数getRoles(){
return $ this-> roles;
}
函数getSubscriptionDetails(){
return $ this-> subscriptionDetails;
}
函数setDepartments(Department $ departments){
$ this-> departments = $ departments;
}
函数setDivisions(Division $ divisions){
$ this-> divisions = $ divisions;
}
函数setEmploymentStatuses(\Benerite\EmployeeBundle\Entity\EmployeeEmploymentStatus $ employmentStatuses){
$ this-> employmentStatuses = $ employmentStatuses;
}
函数setJobTitles(JobTitle $ jobTitles){
$ this-> jobTitles = $ jobTitles;
}
函数setCompanyLocations(位置$ companyLocations){
$ this-> companyLocations = $ companyLocations;
}
函数setRemunerationChangeReasons(RemunerationChangeReason $ remunerationChangeReasons){
$ this-> remunerationChangeReasons = $ remunerationChangeReasons;
}
函数setRoles(Role $ roles){
$ this-> roles = $ roles;
}
函数setSubscriptionDetails(SubscriptionDetail $ subscriptionDetails){
$ this-> subscriptionDetails = $ subscriptionDetails;
}
/ **
* @var整数
*
* @ 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)
* /
私人$ createdDate;
/ **
* @var \DateTime
*
* @ ORM\Column(name =last_updated_date,type =datetime)
* /
private $ lastUpdatedDate;
/ **
*获取ID
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}
/ **
*设置公司名称
*
* @param string $ companyName
*
* @return公司
* /
public function setCompanyName($ companyName)
{
$ this-> companyName = $ companyName;
return $ this;
}
/ **
*获取公司名称
*
* @return string
* /
public function getCompanyName )
{
return $ this-> companyName;
}
/ **
*设置companyRegCode
*
* @param string $ companyRegCode
*
* @return公司
* /
public function setCompanyRegCode($ companyRegCode)
{
$ this-> companyRegCode = $ companyRegCode;
return $ this;
}
/ **
*获取companyRegCode
*
* @return string
* /
public function getCompanyRegCode )
{
return $ this-> companyRegCode;
}
/ **
*设置accountOwner
*
* @param string $ accountOwner
*
* @return公司
* /
public function setAccountOwner($ accountOwner)
{
$ this-> accountOwner = $ accountOwner;
return $ this;
}
/ **
*获取accountOwner
*
* @return string
* /
public function getAccountOwner )
{
return $ this-> accountOwner;
}
/ **
*设置accountEmail
*
* @param string $ accountEmail
*
* @return公司
* /
public function setAccountEmail($ accountEmail)
{
$ this-> accountEmail = $ accountEmail;
return $ this;
}
/ **
*获取accountEmail
*
* @return string
* /
public function getAccountEmail )
{
return $ this-> accountEmail;
}
/ **
*设置companyUrl
*
* @param string $ companyUrl
*
* @return公司
* /
public function setCompanyUrl($ companyUrl)
{
$ this-> companyUrl = $ companyUrl;
return $ this;
}
/ **
*获取companyUrl
*
* @return string
* /
public function getCompanyUrl )
{
return $ this-> companyUrl;
}
/ **
*设置companyStatus
*
* @param string $ companyStatus
*
* @return公司
* /
public function setCompanyStatus($ companyStatus)
{
$ this-> companyStatus = $ companyStatus;
return $ this;
}
/ **
*获取companyStatus
*
* @return string
* /
public function getCompanyStatus( )
{
return $ this-> companyStatus;
}
/ **
*设置createdDate
*
* @param \DateTime $ createdDate
*
* @return公司
* /
public function setCreatedDate($ createdDate)
{
$ this-> createdDate = $ createdDate;
return $ this;
}
/ **
*获取createdDate
*
* @return \DateTime
* /
public function getCreatedDate()
{
return $ this-> createdDate;
}
/ **
*设置lastUpdatedDate
*
* @param \DateTime $ lastUpdatedDate
*
* @return公司
* /
public function setLastUpdatedDate($ lastUpdatedDate)
{
$ this-> lastUpdatedDate = $ lastUpdatedDate;
return $ this;
}
/ **
*获取lastUpdatedDate
*
* @return \DateTime
* /
public function getLastUpdatedDate()
{
return $ this-> lastUpdatedDate;
}
public function __toString()
{
return(string)$ this-> getId();
}
}
我已经创建了使用这个和它的正确的模式。我已经为这两个实体生成了粗野的形式,它也给了我正确的形式。问题是,在我的部门创建和编辑页面时,它会在渲染时给我一个这样的组合框。
< select id =benerite_companybundle_department_companyname =benerite_companybundle_department [company]>
< option value =>< / option>
< option value =1> 1< / option>
< option value =2> 2< / option>
< / select>
这不是真的我想要的应该像
< select id =benerite_companybundle_department_companyname =benerite_companybundle_department [company]>
< option value =>请选择< / option>
< option value =1> comapny 1< / option>
< option value =2> comapny 2< / option>
< / select>
这是我的new.twig.html文件
{%extends':: base.html.twig'%}
{%block body - %}
< h1>部门创作< / 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')}}>
返回列表
< / a>
< / li>
< / ul>
{%endblock%}
我使用symfony2.7和mysql作为数据库。 / p>
你可以这样做:
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('company','entity',array(
'class'=>'BeneriteCompanyBundle:Company',
'choice_label'=>'companyName'
)
- > add('departmentName')
- > add('departmentStatus')
;
}
我希望这将工作如你所愿。
或者如果你想编写自定义查询通过以下url:
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 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();
}
}
I have created schemas using both this and its correct. I have generated crud forms for both entities and its also giving me the correct forms. The issue is that, in my departments create and edit page, its giving me a combobox like this when rendering
<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>
This is not really what I want it should mbe like
<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>
Here is my new.twig.html file
{% 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 %}
I am using symfony2.7 and mysql as database.
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
这篇关于Symfony2.7显示枝条中的combox项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!