【问题标题】:Symfony sonata - form mapperSymfony 奏鸣曲 - 形式映射器
【发布时间】:2018-02-24 09:56:58
【问题描述】:

在 symfony 奏鸣曲中:

  • 我有一个对象 CONTACT 包含许多 ROLE -

我想在表单映射器中查看所有实体角色: 我的实体角色有 4 个参数。 (标签功能、电话、邮箱等)

实际上我只有一个指向该对象的链接。 (但我想查看实体内部的所有参数)

我在我的 ADMIN 类的表单映射器中尝试这个

 $showMapper
           ->with('CONTACT - FUNCTION')
           ->add('role')
           ->end()

命名空间 AdminBundle\Entity;

使用 Doctrine\Common\Collections\ArrayCollection;

/**
 * Role
 */
class Role
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string(unique=true)
     */
    private $function;

    /**
     * @var int
     */
    private $organisation;

    /**
     * @var string
     */
    private $phone;

    /**
     * @var string
     */
    private $email;

     /**
     * @var int
     */
    private $contact=null;

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


    public function __toString(){
    return sprintf("%s %s", $this->getFunction(), $this->getOrganisation());
    }

    public function getFunction_name()
    {
    return $this->getFunction();
    }




     /**
     * Set contact
     *
     * @param int $contact
     *
     * @return role
     */
    public function setContact($contact)
    {
        $this->contact = $contact;

        return $this;
    }

    /**
     * Get contact
     *
     * @return int
     */
    public function getContact()
    {
        return $this->contact;
    }


    /**
     * Set function
     *
     * @param string $function
     *
     * @return Role
     */
    public function setFunction($function)
    {
        $this->function = $function;

        return $this;
    }

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

    /**
     * Set organisation
     *
     * @param int $organisation
     *
     * @return Role
     */
    public function setOrganisation($organisation)
    {
        $this->organisation = $organisation;

        return $this;
    }

    /**
     * Get organisation
     *
     * @return int
     */
    public function getOrganisation()
    {
        return $this->organisation;
    }

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return Role
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Role
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

}

还有我的教义文件

AdminBundle\Entity\Role:
    type: entity
    table: null
    repositoryClass: AdminBundle\Repository\RoleRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        function:
            type: string
            length: 100
            unique: true
        phone:
            type: string
            length: 100
            nullable: TRUE
        email:
            type: string
            length: 100
            nullable: TRUE
    manyToOne:
        organisation:
            targetEntity: AdminBundle\Entity\Organisation
            joinColumn:
                name: organisation
                referencedColumnName: id
            nullable: TRUE
    manyToMany:
        contact:
            targetEntity: AdminBundle\Entity\Contact
            joinTable:
                name: allrole
                joinColumns:
                    role:
                        referencedColumnName: id
                inverseJoinColumns:
                    contact:
                        referencedColumnName: id

【问题讨论】:

    标签: symfony sonata


    【解决方案1】:

    在您的 Role 实体中创建一个 __toString() 方法,并使用您要显示的属性。

    public function __toString()
    {
        return (string) "Function: " . $this->function . ", Phone: " . $this->phone . ", Email: " . $this->email;
    }
    

    【讨论】:

    • 这是一个关系倍数。当我这样做时,什么都没有出现。它只是写:角色名称等......但有一个空格
    • 我把我的实体放在前面
    • 对不起,我没有看到这是针对多对多关系的,我编辑了我的答案。
    • 你能告诉我 to_string 方法的好过程吗?例如实体“角色”的属性“电话”??
    • toString 方法是您的对象的字符串表示形式(不仅仅是一个属性),我已经用一个示例编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-08-11
    • 2018-01-09
    • 2017-04-13
    • 2018-10-15
    • 2018-03-22
    • 1970-01-01
    • 2018-06-18
    • 2017-11-17
    相关资源
    最近更新 更多