本文介绍了在Symfony2中的组合框的值和文本字段中显示数据库中的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 company 的表与部门表具有一对多的关系。我通过指定关系创建了使用Doctrine生成器的两个实体。我也生成了模式,一切都很好

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

请看看我的实体

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;

    /**
     * @var employeeBasicInfo
     *
     * @ORM\OneToMany(targetEntity="Benerite\EmployeeBundle\Entity\EmployeeBasicInfo", 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(\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;
    }

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

    function setEmployeeBasicInfo(\Benerite\EmployeeBundle\Entity\EmployeeBasicInfo $employeeBasicInfo) {
        $this->employeeBasicInfo = $employeeBasicInfo;
    }

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

Department.php / p>

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


}

作为下一个一步,我为部门实体公司实体生成了 CRUD 表单。现在我可以在这两个实体上进行CRUD操作,一切顺利。这对我来说也是可行的

As a next step, I have generated CRUD forms for my Department entity and Company Entity. Now I can make CRUD operations on both the entities and everything goes fine. That's also OK for me

请查看我的CompanyType.php和DepartmentType.php

Please take a look at my CompanyType.php and DepartmentType.php

CompanyType.php

<?php

namespace Benerite\CompanyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CompanyType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('companyName')
            ->add('companyRegCode')
            ->add('accountOwner')
            ->add('accountEmail')
            ->add('companyUrl')
            ->add('companyStatus')
            ->add('createdDate')
            ->add('lastUpdatedDate')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Benerite\CompanyBundle\Entity\Company'
        ));
    }

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

DepartmentType.php / p>

DepartmentType.php

<?php

namespace Benerite\CompanyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

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

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Benerite\CompanyBundle\Entity\Department'
        ));
    }

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

我现在可以创建公司。对于这些创建的公司,我想创建也很好的部门

I can now create Company. For these created companies, I want to create departments that's also goes fine

当渲染部门创建和编辑表单时,标记为组合框项目的公司正在显示公司ID中的文字和值。我想在文本字段和公司ID中显示公司名称的价值属性。

When rendering the departments "create" and "edit" forms, the company labelled combobox items are showing company ID in its text and value. I want to show the company name in text field and company ID in value property.

这是我公司创建的TWIG文件

Here is my company creation TWIG file

new.html.ywig

{% 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 %}

有没有任何选项可以从twig文件或来自DepartmentType.php文件?

Is there any option to customize this from the twig file or from the DepartmentType.php file?

如何实现?

推荐答案

你有两个解决方案。最简单的解决方案:

You have two solution. Th most simple with your current solution :

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

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

        <div>
            {{ form_label(form.company, form.company.companyName) }}
            {{ 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 %}

我不知道如何访问companyName也许你必须在 forms.company.vars 的vars中给出索引,你可以尝试

Im' not sure of how to access to the companyName (maybe you have to give the index in the vars of forms.company.vars. You can try it

或您可以在form_builder中执行此操作:

Or you can do it in your form_builder :

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

这篇关于在Symfony2中的组合框的值和文本字段中显示数据库中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:11