<small id='ngu4k'></small><noframes id='ngu4k'>

<legend id='ngu4k'><style id='ngu4k'><dir id='ngu4k'><q id='ngu4k'></q></dir></style></legend>

      <tfoot id='ngu4k'></tfoot>
      • <bdo id='ngu4k'></bdo><ul id='ngu4k'></ul>

    1. <i id='ngu4k'><tr id='ngu4k'><dt id='ngu4k'><q id='ngu4k'><span id='ngu4k'><b id='ngu4k'><form id='ngu4k'><ins id='ngu4k'></ins><ul id='ngu4k'></ul><sub id='ngu4k'></sub></form><legend id='ngu4k'></legend><bdo id='ngu4k'><pre id='ngu4k'><center id='ngu4k'></center></pre></bdo></b><th id='ngu4k'></th></span></q></dt></tr></i><div id='ngu4k'><tfoot id='ngu4k'></tfoot><dl id='ngu4k'><fieldset id='ngu4k'></fieldset></dl></div>

      教义 2 中与单个实体的父子关系

      Parent Child Relationship With A Single Entity In Doctrine 2(教义 2 中与单个实体的父子关系)
        <bdo id='WFPnr'></bdo><ul id='WFPnr'></ul>

              <tbody id='WFPnr'></tbody>

            <small id='WFPnr'></small><noframes id='WFPnr'>

          1. <i id='WFPnr'><tr id='WFPnr'><dt id='WFPnr'><q id='WFPnr'><span id='WFPnr'><b id='WFPnr'><form id='WFPnr'><ins id='WFPnr'></ins><ul id='WFPnr'></ul><sub id='WFPnr'></sub></form><legend id='WFPnr'></legend><bdo id='WFPnr'><pre id='WFPnr'><center id='WFPnr'></center></pre></bdo></b><th id='WFPnr'></th></span></q></dt></tr></i><div id='WFPnr'><tfoot id='WFPnr'></tfoot><dl id='WFPnr'><fieldset id='WFPnr'></fieldset></dl></div>
                <legend id='WFPnr'><style id='WFPnr'><dir id='WFPnr'><q id='WFPnr'></q></dir></style></legend>
              • <tfoot id='WFPnr'></tfoot>

                本文介绍了教义 2 中与单个实体的父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有如下所示的数据库表:

                <前>+----+--------+-----+|身份证 |家长 |说明 |+----+--------+-----+|1 |空|猫 1 ||2 |1 |P Cat 1 的孩子 1 ||3 |1 |P Cat 1 的孩子 2 ||4 |空|猫 2 ||5 |4 |P Cat 2 的孩子 1 ||6 |4 |P Cat 2 的孩子 2 |+----+--------+-----+

                如何创建具有这些列的学说 2 实体,但我需要父列将id"列作为父列引用.当然,父记录有一个空的父"列值.

                我觉得很公平

                id;}/*** 将另一个类别 ID 设置为该类别的父级.*/公共函数 setParent(Category $category){$this->parent = $category;}/*** 清除父 ID 并使其为空.*/公共函数 clearParent(){$this->parent = null;}/*** 设置描述.** @param 字符串 $description* @return 类别*/公共函数 setDescription($description){$this->description = $description;返回 $this;}/*** 获取描述值.** @return 字符串*/公共函数 getDescription(){返回 $this->description;}}

                不用说,这似乎不起作用.问题是:

                1. 将另一个实体添加为父实体时,setParent() 方法似乎无法按预期工作.
                2. 我需要在该实体上使用 getChildren() 方法.我怎样才能做到这一点?

                解决方案

                这应该有效:

                children = new ArrayCollection();}//一旦你有了它,访问父级和子级应该是直接的//(在本示例中,一旦您尝试访问它们,它们就会被延迟加载).IE:公共函数 getParent() {返回 $this->parent;}公共函数 getChildren() {返回 $this->children;}//...//总是使用它来建立一个新的父/子关系公共函数 addChild(Category $child) {$this->children[] = $child;$child->setParent($this);}公共函数 setParent(Category $parent) {$this->parent = $parent;}}

                I have database table that looks like this:

                +----+--------+--------------------+
                | id | parent | description        |
                +----+--------+--------------------+
                |  1 | null   | P Cat 1            |
                |  2 | 1      | Child 1 of P Cat 1 |
                |  3 | 1      | Child 2 of P Cat 1 |
                |  4 | null   | P Cat 2            |
                |  5 | 4      | Child 1 of P Cat 2 |
                |  6 | 4      | Child 2 of P Cat 2 |
                +----+--------+--------------------+
                

                How can I create a doctrine 2 entity that has these columns, but I need the parent column to reference the "id" column as a parent. Of course, a parent record has a null "parent" column value.

                So fair I have

                <?php
                namespace MyNamespace;
                use DoctrineORMMapping AS ORM;
                use DoctrineCommonCollectionsArrayCollection;
                /**
                 * @ORMEntity
                 * @ORMTable(name="category")
                 **/
                class Category
                {
                    /**
                     * @ORMId
                     * @ORMColumn(type="integer", name="id")
                     * @ORMGeneratedValue
                     */
                    protected $id;
                
                    /**
                     * Creates a parent / child relationship on this entity.
                     *
                     * @ORMManyToOne(targetEntity="MyNamespaceCategory",inversedBy="id")
                     * @ORMJoinColumn(name="FK_parent_id", referencedColumnName="id", nullable=true)
                     */
                    protected $parent = null;
                
                    /**
                     * @ORMColumn(type="string", name="description", length=250)
                     *
                     * @var string
                     */
                    protected $description;
                
                    /**
                     * Gets the Primary key value.
                     *
                     * @return integer
                     */
                    public function getId()
                    {
                        return $this->id;
                    }
                
                    /**
                     * Sets another category ID as the parent of this category.
                     */
                    public function setParent(Category $category)
                    {
                        $this->parent = $category;
                    }
                
                    /**
                     * Clears the parent id and makes it null.
                     */
                    public function clearParent()
                    {
                        $this->parent = null;
                    }
                
                    /**
                     * Sets the description.
                     *
                     * @param string $description
                     * @return Category
                     */
                    public function setDescription($description)
                    {
                        $this->description = $description;
                        return $this;
                    }
                
                    /**
                     * Gets the description value.
                     *
                     * @return string
                     */
                    public function getDescription()
                    {
                        return $this->description;
                    }
                }
                

                Needless to say, this doesn't appear to work. The questions are:

                1. The setParent() method doesn't appear to work as expected when another entity is added as a parent.
                2. I need a getChildren() method on this entity. How can I achieve that?

                解决方案

                This should work:

                <?php
                
                use DoctrineCommonCollectionsArrayCollection;
                
                /** @ORMEntity */
                class Category {
                
                    /**
                     * @ORMId
                     * @ORMColumn(type="integer", name="id")
                     * @ORMGeneratedValue
                     */
                    protected $id;
                
                    // ...
                
                    /**
                     * @ORMOneToMany(targetEntity="Category", mappedBy="parent")
                     */
                    protected $children;
                
                    /**
                     * @ORMManyToOne(targetEntity="Category", inversedBy="children")
                     * @ORMJoinColumn(name="parent", referencedColumnName="id")
                     */
                    protected $parent;
                
                    public function __construct() {
                        $this->children = new ArrayCollection();
                    }
                
                    // Once you have that, accessing the parent and children should be straight forward 
                    // (they will be lazy-loaded in this example as soon as you try to access them). IE:
                
                    public function getParent() {
                        return $this->parent;
                    }
                
                    public function getChildren() {
                        return $this->children;
                    }
                
                    // ...
                
                    // always use this to setup a new parent/child relationship
                    public function addChild(Category $child) {
                       $this->children[] = $child;
                       $child->setParent($this);
                    }
                
                    public function setParent(Category $parent) {
                       $this->parent = $parent;
                    }
                
                }
                

                这篇关于教义 2 中与单个实体的父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                How to parse django style template tags(如何解析 django 样式模板标签)
                What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                php in background exec() function(php 后台 exec() 函数)
                <i id='CJIcv'><tr id='CJIcv'><dt id='CJIcv'><q id='CJIcv'><span id='CJIcv'><b id='CJIcv'><form id='CJIcv'><ins id='CJIcv'></ins><ul id='CJIcv'></ul><sub id='CJIcv'></sub></form><legend id='CJIcv'></legend><bdo id='CJIcv'><pre id='CJIcv'><center id='CJIcv'></center></pre></bdo></b><th id='CJIcv'></th></span></q></dt></tr></i><div id='CJIcv'><tfoot id='CJIcv'></tfoot><dl id='CJIcv'><fieldset id='CJIcv'></fieldset></dl></div>
                  • <legend id='CJIcv'><style id='CJIcv'><dir id='CJIcv'><q id='CJIcv'></q></dir></style></legend>

                      <small id='CJIcv'></small><noframes id='CJIcv'>

                          <bdo id='CJIcv'></bdo><ul id='CJIcv'></ul>
                          <tfoot id='CJIcv'></tfoot>

                            <tbody id='CJIcv'></tbody>