<tfoot id='7noTi'></tfoot>

    • <bdo id='7noTi'></bdo><ul id='7noTi'></ul>
    1. <legend id='7noTi'><style id='7noTi'><dir id='7noTi'><q id='7noTi'></q></dir></style></legend>

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

    2. <small id='7noTi'></small><noframes id='7noTi'>

    3. 学说2中的问题理解关系映射

      时间:2024-08-09

          <bdo id='1ljdl'></bdo><ul id='1ljdl'></ul>
        • <small id='1ljdl'></small><noframes id='1ljdl'>

            <tbody id='1ljdl'></tbody>
        • <tfoot id='1ljdl'></tfoot>

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

              • 本文介绍了学说2中的问题理解关系映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我阅读了官方文档和大量线程,但仍然没有找到适合我情况的解决方案.我的情况很基本.我有 2 个实体:它们的评论和关键字.一条评论可以有多个关键词,但每个关键词只能用于一条评论.关键字在关键字表中不是唯一的.所以我决定这是一对多的关系.表结构简单如下:

                I read official documentation and tons of threads but still do not find solution for my situation. My case is very basic. I have 2 entities: comments and keywords for them. One comment can have many keywords but each keyword is only for one comment. Keywords are not unique in keyword table. So i decided this is one-to-many relation. Tables structure are simply like follows:

                关键字

                id          int(11)
                comment_id  int(11)
                text        varchar(30)
                

                评论

                id      int(11)
                text    text
                

                这是我绘制它们的方式:

                here is how i mapped them:

                
                /**
                 *  @Entity
                 *  @Table(name="comments")
                 **/
                class Comments
                {
                    /** @Id @Column(type="integer") */
                    private $id;
                    /** @Column(type="text") */
                    private $text;
                
                    /**
                     * @OneToMany(targetEntity="keywords", mappedBy="comment_id")
                     */
                    private $keywords;
                
                    public function getText(){return $this->text;}
                    public function getId(){return $this->id;}
                    public function getKeywords(){return $this->keywords;}
                }
                /**
                 *  @Entity
                 *  @Table(name="keywords")
                 */
                
                class Keywords
                {
                    /** @Id @Column(type="integer") */
                    private $id;
                
                    private $text;
                
                    public function getText(){return $this->text;}
                    public function getId(){return $this->id;}
                }
                
                

                以及如何使用它是这样的:

                and how using it is like this:

                
                $comments = $this->em->getRepository('comments' )->findAll();
                foreach($comments as $comment){
                    foreach($comment->getKeywords() as $keyword){
                        $keyword->getText();
                    }
                }

                并得到这个错误:<代码>

                and got this errors:

                
                Notice: Undefined index: comment_id in C:web_includesdoctrineORMPersistersBasicEntityPersister.php on line 1096
                Notice: Trying to get property of non-object in C:web_includesdoctrineORMPersistersBasicEntityPersister.php on line 1098
                Warning: Invalid argument supplied for foreach() in C:web_includesdoctrineORMPersistersBasicEntityPersister.php on line 1098
                Notice: Undefined index: comment_id in C:web_includesdoctrineORMPersistentCollection.php on line 168
                Fatal error: Call to a member function setValue() on a non-object in C:web_includesdoctrineORMPersistentCollection.php on line 169
                

                怎么了?应该在哪里定义comment_id?我的映射正确吗?我真的卡住了,需要帮助,所以非常欢迎任何建议.

                What is wrong? where is should define comment_id? Is my mapping correct? i really stuck and need help so please, any advice are very welcome.

                推荐答案

                mappedBy 属性并没有说明外键的名称,这就是@JoinColumn"注解的用途.这种情况的正确映射是:

                The mappedBy attribute does say nothing about the name of the foreign key, That is what the "@JoinColumn" annotation is for. The correct mapping for this scenario would be:

                /**
                 *  @Entity
                 *  @Table(name="comments")
                 **/
                class Comments
                {
                    /** @Id @Column(type="integer") */
                    private $id;
                    /** @Column(type="text") */
                    private $text;
                
                    /**
                     * @OneToMany(targetEntity="keywords", mappedBy="comment")
                     */
                    private $keywords;
                
                    public function getText(){return $this->text;}
                    public function getId(){return $this->id;}
                    public function getKeywords(){return $this->keywords;}
                }
                
                /**
                 *  @Entity
                 *  @Table(name="keywords")
                 */
                class Keywords
                {
                    /** @Id @Column(type="integer") */
                    private $id;
                
                    /**
                     * @ManyToOne(targetEntity="Comments", inversedBy="keywords")
                     */
                    private $comment;
                
                    /**
                     * @Column(type="text") */
                    private $text;
                
                    public function getText(){return $this->text;}
                    public function getId(){return $this->id;}
                }
                

                使用 Schema Tool 它会生成与您的架构相同的 SQL:

                Using Schema Tool it generates the SQL which equals your schema:

                CREATE TABLE comments (id INT NOT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
                CREATE TABLE keywords (id INT NOT NULL, comment_id INT DEFAULT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
                ALTER TABLE keywords ADD FOREIGN KEY (comment_id) REFERENCES comments(id);
                

                映射中的两个问题:

                1. 您必须了解拥有方和反方之间的区别.仅仅拥有一个一对多的单向关系就需要第三个连接表.但是,在我与拥有方属性 Keyword::$comment 的映射中,它可以工作.
                2. mappedBy"指的是另一个 targetEntity 上的属性,即此关联的另一端".InversedBy 的含义有些相同,只是 inversedBy 始终在拥有方指定,mappedBy 在反方指定.

                这听起来很复杂,但从 ORM 技术的角度来看,这是一种非常有效的方法,因为它允许使用最少数量的 SQL UPDATE 语句来更新关联.请参阅有关 Inverse/Owning 工作原理的文档:

                This all sounds very complicated, but its a very efficient way from the ORMs technical perspective, because it allows to update associations with the least number of required SQL UPDATE statements. See the docs on how Inverse/Owning works:

                http://www.doctrine-project.org/projects/orm/2.0/docs/reference/association-mapping/en#owning-side-and-inverse-side

                这篇关于学说2中的问题理解关系映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Doctrine 2 多对多级联 下一篇:Symfony 3 连接到多个数据库

                相关文章

                  <bdo id='dlj5T'></bdo><ul id='dlj5T'></ul>

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

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

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