<tfoot id='3NQsi'></tfoot>

        <legend id='3NQsi'><style id='3NQsi'><dir id='3NQsi'><q id='3NQsi'></q></dir></style></legend>

          <bdo id='3NQsi'></bdo><ul id='3NQsi'></ul>

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

        <small id='3NQsi'></small><noframes id='3NQsi'>

        多对多插入原则

        Doctrine Many to Many Insert(多对多插入原则)

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

            <small id='0K1KW'></small><noframes id='0K1KW'>

            1. <legend id='0K1KW'><style id='0K1KW'><dir id='0K1KW'><q id='0K1KW'></q></dir></style></legend>

                • 本文介绍了多对多插入原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些问题.我正在研究并尝试所有建议但没有一个工作.

                  i have some promblem. i was research and try all suggest but no one work.

                  我最终得到:

                  传递给 EntityUser::addCategories() 的参数 1 必须是 EntityCategory 的实例,给定字符串,

                  我有很多关系,用户、用户类别和类别

                  i have manytomany relationship, user, user_category, and category

                  用户

                  <?php
                  
                  namespace Entity;
                  
                  use DoctrineCommonCollectionsArrayCollection;
                  
                  use GedmoMappingAnnotation as Gedmo;
                  use DoctrineORMMapping as ORM;
                  
                  /**
                   * @Entity
                   * @Table(name="user")
                   */
                  class User
                  {
                  
                      /**
                       * @Id
                       * @Column(type="integer", nullable=false)
                       * @GeneratedValue(strategy="AUTO")
                       */
                      public $id;
                  
                      /**
                       * @Column(type="string", length=255, unique=true, nullable=false)
                       */
                      public $name;
                  
                  
                      /**
                       * @ManyToMany(targetEntity="EntityCategory", inversedBy="user")
                       * @ORMJoinTable(name="user_category")
                       */
                      public $categories;
                  
                          public function __construct() {
                              $this->category = new DoctrineCommonCollectionsArrayCollection();
                      }
                  
                      public function getCategories()
                      {
                          return $this->categories;
                      }
                  
                      public function addCategories(Category $category = null)
                      {
                          $this->categories = $category;
                      }
                  
                      public function setName($name)
                      {
                          $this->name = $name;
                      }
                      public function getName()
                      {
                          return $this->name;
                      }
                  
                  }
                  

                  类别

                  <?php
                  
                  namespace Entity;
                  
                  use DoctrineCommonCollectionsArrayCollection;
                  
                  use GedmoMappingAnnotation as Gedmo;
                  use DoctrineORMMapping as ORM;
                  
                  /**
                   * @Entity
                   * @Table(name="category")
                   */
                  class Category
                  {
                  
                      /**
                       * @Id
                       * @Column(type="integer", nullable=false)
                       * @GeneratedValue(strategy="AUTO")
                       */
                      public $id;
                  
                      /**
                       * @Column(type="string", length=255, unique=true, nullable=false)
                       */
                      public $name;
                  
                      /**
                       * @ManyToMany(targetEntity="EntityUser", mappedBy="category")
                       */
                      public $user;
                  
                  
                      public function __construct() {
                          $this->user = new DoctrineCommonCollectionsArrayCollection();
                      }
                  
                      public function getUser()
                      {
                          return $this->user;
                      }
                  
                      public function addUser(User $user = null)
                      {
                          $user->addCategory($this);
                          $this->user = $user;
                      }
                  
                  
                      public function setName($name)
                      {
                          $this->name = $name;
                      }
                      public function getName()
                      {
                      return $this->name;
                      }
                  }
                  

                  插入函数

                          // check existence object in database
                          $res = $this->em->find('EntityUser', $this->input->post('id'));
                  
                          if($res){
                              $data = $this->em->find('EntityUser', $this->input->post('id'));
                          }else{
                              // create a new User object
                              $data = new EntityUser;                
                          }
                  
                  
                          $data->setName($this->input->post('name'));
                          $data->addCategories($this->input->post('category'));
                  
                  
                          // save the data object to the database
                          $this->em->persist($data);
                  
                          $this->em->flush();
                  

                  get 一切顺利,但我对开始工作感到很困惑.

                  Everything goes fine on get but i'm so confuse for set to work.

                  感谢您的帮助.对不起我的英语.

                  thanks for your help. sorry for my english.

                  推荐答案

                  你有很多错误(注意语法):

                  You have lot of errors (pay attention to grammar):

                  代替

                  public $categories;
                  public function __construct() {
                      $this->category = new DoctrineCommonCollectionsArrayCollection();
                  }
                  

                  应该是:

                  protected $categories;
                  
                  public function __construct() {
                      $this->categories = new DoctrineCommonCollectionsArrayCollection();
                  }
                  

                  代替:

                  public $user;
                  public function __construct() {
                      $this->user = new DoctrineCommonCollectionsArrayCollection();
                  }
                  

                  使用

                  protected $users;
                  public function __construct() {
                      $this->users = new DoctrineCommonCollectionsArrayCollection();
                  }
                  

                  代替

                  public function addCategories(Category $category = null)
                  {
                      $this->categories = $category;
                  }
                  

                  一定是

                  public function addCategory(Category $category = null)
                  {
                      $this->categories->add($category);
                  }
                  

                  public function removeCategory(Category $category)
                  {
                      $this->categories->removeElement($category) ;
                  }
                  public function setCategories($categories)
                  {
                      $this->categories = categories;
                  }
                  

                  双方的逻辑相同.我不知道 CI 是如何工作的,但 Symfony 会自动找到 addSomething/removeSomething 方法.即使 CI 不支持该功能,您仍应按上述方式更改代码.

                  Same logic on both sides. I don't know how CI works but Symfony will automatically find addSomething/removeSomething methods. Even if CI doesn't support that feature, you should still change your code as above.

                  这篇关于多对多插入原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 函数)
                  • <small id='ecjho'></small><noframes id='ecjho'>

                      <tbody id='ecjho'></tbody>

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

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

                        <tfoot id='ecjho'></tfoot>