sql >> データベース >  >> RDS >> Mysql

教義2における単一の実体との親子関係

    これは機能するはずです:

    <?php
    
    use Doctrine\Common\Collections\ArrayCollection;
    
    /** @ORM\Entity */
    class Category {
    
        /**
         * @ORM\Id
         * @ORM\Column(type="integer", name="id")
         * @ORM\GeneratedValue
         */
        protected $id;
    
        // ...
    
        /**
         * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
         */
        protected $children;
    
        /**
         * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
         * @ORM\JoinColumn(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;
        }
    
    }
    


    1. MySQL Workbenchは外部キーのインデックスを自動的に作成しますか?

    2. SQLデータベースにアラビア文字を挿入する方法は?

    3. mysql_real_escape_string()を使用する場合

    4. 階層型MySQLクエリのヘルプが必要