php - 学说一对一关系映射

标签 php mysql entity-framework symfony doctrine-orm

我有两个实体,我想映射它们,这样当我为每个实体生成 crud 时,当我想使用 Map 实体进行新插入时,能够根据 Board 实体的 id 选择。 我一直在尝试进行映射......我不确定我是否做对了,因为在 mysql 中,我在生成 sql 后看不到外键。

Board.php

    <?php

    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * Board
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="AppBundle\Entity\BoardRepository")
     */
    class Board
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\ManyToOne(targetEntity="Board")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id", referencedColumnName="BoardId")
         * })

         */
        private $id;

        /**
         * @var string
         *
         * @ORM\Column(name="PropertyName", type="string", length=255)
         */
        private $propertyName;

        /**
         * @var string
         *
         * @ORM\Column(name="PropertyDescription", type="string", length=255)
         */
        private $propertyDescription;

        /**
         * @var string
         *
         * @ORM\Column(name="PropertyPicture", type="string", length=255)
         */
        private $propertyPicture;

        /**
         * @var string
         *
         * @ORM\Column(name="PropertyGlyphonic", type="string", length=255)
         */
        private $propertyGlyphonic;

        /**
         * @var string
         *
         * @ORM\Column(name="PropertyPrice", type="string", length=255)
         */
        private $propertyPrice;


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

        /**
         * Set propertyName
         *
         * @param string $propertyName
         * @return Board
         */
        public function setPropertyName($propertyName)
        {
            $this->propertyName = $propertyName;

            return $this;
        }

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

        /**
         * Set propertyDescription
         *
         * @param string $propertyDescription
         * @return Board
         */
        public function setPropertyDescription($propertyDescription)
        {
            $this->propertyDescription = $propertyDescription;

            return $this;
        }

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

        /**
         * Set propertyPicture
         *
         * @param string $propertyPicture
         * @return Board
         */
        public function setPropertyPicture($propertyPicture)
        {
            $this->propertyPicture = $propertyPicture;

            return $this;
        }

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

        /**
         * Set propertyGlyphonic
         *
         * @param string $propertyGlyphonic
         * @return Board
         */
        public function setPropertyGlyphonic($propertyGlyphonic)
        {
            $this->propertyGlyphonic = $propertyGlyphonic;

            return $this;
        }

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

        /**
         * Set propertyPrice
         *
         * @param string $propertyPrice
         * @return Board
         */
        public function setPropertyPrice($propertyPrice)
        {
            $this->propertyPrice = $propertyPrice;

            return $this;
        }

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

Map.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Map
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\MapRepository")
 */
class Map
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     * @ORM\Column(name="BoardId", type="integer")
     */
    private $boardId;

    /**
     * @var string
     *
     * @ORM\Column(name="X", type="string", length=255)
     */
    private $x;

    /**
     * @var string
     *
     * @ORM\Column(name="Y", type="string", length=255)
     */
    private $y;


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

    /**
     * Set boardId
     *
     * @param integer $boardId
     * @return Map
     */
    public function setBoardId($boardId)
    {
        $this->boardId = $boardId;

        return $this;
    }

    /**
     * Get boardId
     *
     * @return integer 
     */
    public function getBoardId()
    {
        return $this->boardId;
    }

    /**
     * Set x
     *
     * @param string $x
     * @return Map
     */
    public function setX($x)
    {
        $this->x = $x;

        return $this;
    }

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

    /**
     * Set y
     *
     * @param string $y
     * @return Map
     */
    public function setY($y)
    {
        $this->y = $y;

        return $this;
    }

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

最佳答案

看来您还没有阅读手册。它在 Symfony.com 和 Doctrine-orm.readthedocs.org 上都是很棒的文档。

阅读这篇文章 ( http://symfony.com/doc/current/book/doctrine.html ) 并将这篇文章用作引用 ( http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html ),您将找到这个问题和许多 future 问题的答案。

你基本上需要做的是:

 class Board {
   /**         
    * @ORM\ManyToOne(targetEntity="Map")
    */
   private $map;
 }

关于php - 学说一对一关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27780996/

相关文章:

php - 在 php 文件的 JavaScript 中转义 <?php ?>

php - BigINT 和 VarChar 的区别,长度 16 个字符

c# - AddOrUpdate 不修改 child

c# - LINQ 表达式未被转换为 SQL

c# - ef4 导致 Web 服务中的循环引用

php - Laravel 从查询构建器获取特定列

php从mysql数据库中获取多个数组并序列化数组

php - SQL Where NOT Exists 插入

mysql - 如何在触发操作中使用WHEN条件

Mysql过滤双日期