在Doctrine 2.2中体现MySQL多态关联

2024-03-17

我将 Doctrine 2.2.0 与 Codeigniter 一起使用。我是 Doctrine(或者一般的 ORM)的新手。

我正在基于 YAML 文件设置实体和代理类,效果很好。我在 Doctrine 类中反映数据库中的多态关联时确实遇到问题。我正在寻找有关如何在 Doctrine 中实现以下多态关联的具体示例。

在我的数据库中,我有一个名为产品的表。取决于字段的值object_type and object_id我想与桌上视频或桌上汽车中的记录相关(我在这里简化了它)。我想将两种产品类型保留在两个单独的表中,因为一个与另一个无关,并且两个表都与其他表相关。

我查看了 Doctrine Inheritance 文档和其他示例,但它似乎对我没有帮助。如果可能的话我想避免添加列description and price到桌子上的视频和汽车.

非常感谢!

|Table: products                                      |
|-----------------------------------------------------|
| ID | description | price  | object_type | object_id |
|-----------------------------------------------------|
| 1  | A video     | 20.00  | video       | 12        |
| 2  | A car       | 159.00 | car         | 5         |

|Table: videos                               |
|--------------------------------------------|
| ID | filename     | artist_id | date       |
|--------------------------------------------|
| 12 | somename.mp4 | 189       | 2011-02-15 |

|Table: cars                   |
|------------------------------|
| ID | brand_id | model | year |
|------------------------------|
| 5  | 17       | astra | 2010 |

我自己找到了解决方案。我在下面列出了我已完成的不同步骤。这个答案很大,但我试图尽可能完整。

最重要的东西在 Product YAML 文件中inheritanceType: JOINED, discriminatorColumn:, discriminatorMap:以及 Video 和 Car 实体类class Video extends Product, class Car extends Product.

1)数据库架构

CREATE TABLE IF NOT EXISTS `artists` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `brands` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `cars` (
  `id` int(11) NOT NULL,
  `model` varchar(255) NOT NULL,
  `release_date` date NOT NULL,
  `brand_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) NOT NULL,
  `price` double NOT NULL,
  `object_type` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `videos` (
  `id` int(11) NOT NULL,
  `file_name` varchar(255) NOT NULL,
  `release_date` date NOT NULL,
  `artist_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

2) Doctrine YAML 文件(您需要从中创建实体和代理类)

实体.Artist.dcm.yml

Entities\Artist:
    type: entity
    table: artists
    id:
        id:
            type: integer
            primary: true
            notnull: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string(255)
            notnull: true
    oneToMany:
        videos:
            targetEntity: Video
            mappedBy: artist
    options:
        charset: utf8
        type: InnoDB

实体.Brand.dcm.yml

Entities\Brand:
    type: entity
    table: brands
    id:
        id:
            type: integer
            primary: true
            notnull: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string(255)
            notnull: true
    oneToMany:
        cars:
            targetEntity: Car
            mappedBy: brand
    options:
        charset: utf8
        type: InnoDB

实体.Car.dcm.yml

Entities\Car:
    type: entity
    table: cars
    fields:
        model:
            type: string
            notnull: true
        release_date:
            type: date
            notnull: true
    oneToOne: #unidirectional    
        brand:
            targetEntity: Brand
            joinColumns:
                brand_id:
                    referencedColumnName: id
    options:
        charset: utf8
        type: InnoDB

实体.产品.dcm.yml

Entities\Product:
    type: entity
    table: products
    id:
        id:
            type: string
            primary: true
            notnull: true
            generator:
                strategy: AUTO
    fields:
        description:
            type: string(255)
            notnull: true
        price:
            type: decimal
            notnull: true
    inheritanceType: JOINED
    discriminatorColumn:
        name: object_type
        type: string
        length: 255
    discriminatorMap:
        video: Video
        car: Car
    options:
        charset: utf8
        type: InnoDB

实体.Video.dcm.yml

Entities\Video:
    type: entity
    table: videos
    fields:
        file_name:
            type: string
            notnull: true
        release_date:
            type: date
            notnull: true
    oneToOne: #unidirectional    
        artist:
            targetEntity: Artist
            joinColumns:
                artist_id:
                    referencedColumnName: id
    options:
        charset: utf8
        type: InnoDB

3)创建Entity和Proxy类

我使用CodeIgniter。我使用了 Joel Verhagen 的优秀指南 http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/

!!使用产品扩展视频和汽车非常重要 - 见下文!

这会产生以下实体类

艺术家.php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Artist
 *
 * @ORM\Table(name="artists")
 * @ORM\Entity
 */
class Artist
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Entities\Video", mappedBy="artist")
     */
    private $videos;

    public function __construct()
    {
        $this->videos = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set name
     *
     * @param string $name
     * @return Artist
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

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

    /**
     * Add videos
     *
     * @param Entities\Video $videos
     */
    public function addVideo(\Entities\Video $videos)
    {
        $this->videos[] = $videos;
    }

    /**
     * Get videos
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getVideos()
    {
        return $this->videos;
    }
}

品牌.php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Brand
 *
 * @ORM\Table(name="brands")
 * @ORM\Entity
 */
class Brand
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Entities\Car", mappedBy="brand")
     */
    private $cars;

    public function __construct()
    {
        $this->cars = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set name
     *
     * @param string $name
     * @return Brand
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

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

    /**
     * Add cars
     *
     * @param Entities\Car $cars
     */
    public function addCar(\Entities\Car $cars)
    {
        $this->cars[] = $cars;
    }

    /**
     * Get cars
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getCars()
    {
        return $this->cars;
    }
}

Car.php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Car
 *
 * @ORM\Table(name="cars")
 * @ORM\Entity
 */
class Car extends Product //Important that Car extends Product
{
    /**
     * @var string $model
     *
     * @ORM\Column(name="model", type="string")
     */
    private $model;

    /**
     * @var date $release_date
     *
     * @ORM\Column(name="release_date", type="date")
     */
    private $release_date;

    /**
     * @var Entities\Brand
     *
     * @ORM\OneToOne(targetEntity="Entities\Brand")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="brand_id", referencedColumnName="id", unique=true)
     * })
     */
    private $brand;


    /**
     * Set model
     *
     * @param string $model
     * @return Car
     */
    public function setModel($model)
    {
        $this->model = $model;
        return $this;
    }

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

    /**
     * Set release_date
     *
     * @param date $releaseDate
     * @return Car
     */
    public function setReleaseDate($releaseDate)
    {
        $this->release_date = $releaseDate;
        return $this;
    }

    /**
     * Get release_date
     *
     * @return date 
     */
    public function getReleaseDate()
    {
        return $this->release_date;
    }

    /**
     * Set brand
     *
     * @param Entities\Brand $brand
     * @return Car
     */
    public function setBrand(\Entities\Brand $brand = null)
    {
        $this->brand = $brand;
        return $this;
    }

    /**
     * Get brand
     *
     * @return Entities\Brand 
     */
    public function getBrand()
    {
        return $this->brand;
    }
}

产品.php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Product
 *
 * @ORM\Table(name="products")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="", type="", length=)
 * @ORM\DiscriminatorMap({"video" = "Entities\Video", "car" = "Entities\Car"})
 * @ORM\Entity
 */
class Product
{
    /**
     * @var string $id
     *
     * @ORM\Column(name="id", type="string")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var decimal $price
     *
     * @ORM\Column(name="price", type="decimal")
     */
    private $price;


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

    /**
     * Set description
     *
     * @param string $description
     * @return Product
     */
    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }

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

    /**
     * Set price
     *
     * @param decimal $price
     * @return Product
     */
    public function setPrice($price)
    {
        $this->price = $price;
        return $this;
    }

    /**
     * Get price
     *
     * @return decimal 
     */
    public function getPrice()
    {
        return $this->price;
    }
}

视频.php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Video
 *
 * @ORM\Table(name="videos")
 * @ORM\Entity
 */
class Video extends Product //Important that Video extends Product
{
    /**
     * @var string $file_name
     *
     * @ORM\Column(name="file_name", type="string")
     */
    private $file_name;

    /**
     * @var date $release_date
     *
     * @ORM\Column(name="release_date", type="date")
     */
    private $release_date;

    /**
     * @var Entities\Artist
     *
     * @ORM\OneToOne(targetEntity="Entities\Artist")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="artist_id", referencedColumnName="id", unique=true)
     * })
     */
    private $artist;


    /**
     * Set file_name
     *
     * @param string $fileName
     * @return Video
     */
    public function setFileName($fileName)
    {
        $this->file_name = $fileName;
        return $this;
    }

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

    /**
     * Set release_date
     *
     * @param date $releaseDate
     * @return Video
     */
    public function setReleaseDate($releaseDate)
    {
        $this->release_date = $releaseDate;
        return $this;
    }

    /**
     * Get release_date
     *
     * @return date 
     */
    public function getReleaseDate()
    {
        return $this->release_date;
    }

    /**
     * Set artist
     *
     * @param Entities\Artist $artist
     * @return Video
     */
    public function setArtist(\Entities\Artist $artist = null)
    {
        $this->artist = $artist;
        return $this;
    }

    /**
     * Get artist
     *
     * @return Entities\Artist 
     */
    public function getArtist()
    {
        return $this->artist;
    }
}

3) 创建一辆新车和一个新视频

这就是我的 CodeIgniter 控制器中的内容。该代码假定您已经创建了一位名为 Metallica 的艺术家和一个名为 Ford 的品牌。

public function createVideo()
{
    //Instantiate new Entities\Video object
    $video = new Entities\Video;

    //Since it extends Entities\Product you can set the common Product properties
    $video->setDescription('This is a Metallica clip');
    $video->setPrice(19.95);

    //Setting the custom Video properties
    $artist = $this->doctrine->em->getRepository('Entities\Artist')->findOneBy(array('name' => 'Metallica'));
    $video->setArtist($artist);
    $video->setReleaseDate(new DateTime());
    $video->setFileName('metallica.mp4');

    //Save
    $this->doctrine->em->persist($video);
    $this->doctrine->em->flush();
}

public function createCar()
{
    //Instantiate new Entities\Car object
    $car = new Entities\Car;

    //Since it extends Entities\Product you can set the common Product properties
    $car->setDescription('This is Ford Mondeo of 2011');
    $car->setPrice(19.95);

    //Setting the custom Car properties
    $brand = $this->doctrine->em->getRepository('Entities\Brand')->findOneBy(array('name' => 'Ford'));
    $car->setBrand($brand);
    $car->setReleaseDate(DateTime::createFromFormat('Y-m-d', '2011-11-15'));
    $car->setModel('Mondeo');

    //Save
    $this->doctrine->em->persist($car);
    $this->doctrine->em->flush();
}

4)提取所有产品

有关如何提取所有产品的示例

public function extractAllProducts()
{
    $products = $this->doctrine->em->getRepository('Entities\Product')->findAll();
    foreach($products as $product)
    {
        printf('%s for € %s<br />', $product->getDescription(), $product->getPrice());
    }
}

这导致

This is a Metallica clip for € 19.95
This is Ford Mondeo of 2011 for € 19.95

有关如何提取所有视频的示例

public function extractAllVideos()
{
    $videos = $this->doctrine->em->getRepository('Entities\Video')->findAll();
    foreach($videos as $video)
    {
        printf('%s, released %s for € %s<br />', $video->getDescription(),  $video->getReleaseDate()->format('Y'), $video->getPrice());
    }
}

这导致

This is a Metallica clip, released 2012 for € 19.95
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Doctrine 2.2中体现MySQL多态关联 的相关文章

  • 教义 2 中的关系

    我完全没有理解教义中的关联 我想知道单向和双向关系有什么区别 学说2中的正方和反方是什么 双向和单向关系 双向和单向与 PHP 对象中的引用有关 如你看到的here http www doctrine project org docs or
  • Doctrine Querybuilder ORDER BY 子句不在 SELECT 列表中

    我有以下查询生成器 queryBuilder this gt createQueryBuilder recipient gt leftJoin recipient message message gt orderBy message dat
  • 在Doctrine 2.2中体现MySQL多态关联

    我将 Doctrine 2 2 0 与 Codeigniter 一起使用 我是 Doctrine 或者一般的 ORM 的新手 我正在基于 YAML 文件设置实体和代理类 效果很好 我在 Doctrine 类中反映数据库中的多态关联时确实遇到
  • 教义查询崩溃

    非常非常奇怪 我已经使用这种教义方法数百次了 我有一个简单的控制器 它以 id 作为参数 Doctrine 生成的查询是错误的并且崩溃 Security has role ROLE ADMIN return Response public
  • 原则 2 的 GIS 扩展

    我想为我的 Doctrine 2 项目编写一个 GIS 地理空间数据 扩展 我知道如何编写简单的自定义函数和类型 为了适应MySQL特殊的存储格式 我在检索 存储数据时需要使用一些SQL函数 GeomFromWKB 和AsBinary 我找
  • 单表继承和Yaml配置

    我想在我的项目中使用 symfony2 doctrine 的单表继承 但我找不到任何带有 yaml 配置的工作示例 官方文档中仅提供了注释配置 我找到了 xml 示例 但我想使用 yaml 配置 有人可以帮助并分享一些工作代码吗 好的内置转
  • 学说、命名空间和自动加载实体

    我想在我的项目中使用 Doctrine 2 我有一些问题 我阅读了文档 但可能我做错了什么 我想自动加载实体类 并且文档中的方法不起作用 我的 bootstrap php
  • Doctrine2 将实体导出到数组

    I have Product具有多对一的实体Category实体 我需要商店Product会议中 首先我尝试实施 Serializable产品上的界面 我应该如何序列化我的相关Category实体 我是否也应该实施 Serializable
  • Mysql 的 Doctrine 查询

    我想创建一个 Doctrine 查询 Doctrine 2 3 SELECT FROM car WHERE plate like AND datetime BETWEEN 2013 03 13 22 20 18 AND 2013 03 13
  • 学说2:如何在不丢失数据的情况下将一对多转换为多对多

    在我的应用程序中 我想将一对多转换为多对多而不丢失数据 from ORM ManyToOne targetEntity AppBundle Entity FoodAnalytics Recipe inversedBy medias ORM
  • QueryBuilder/Doctrine 选择加入 groupby

    所以最近我一直在思考 但还找不到这个问题的解决方案 因为我缺乏对doctrine2和symfony查询生成器的开发 我有 2 张桌子 目标 id user id target value 储蓄 id goal id 金额 我需要从目标中进行
  • Symfony2 覆盖 User.orm.xml

    我需要覆盖这个 Symfony vendor friendsofsymfony user bundle FOS UserBundle Resources config doctrine model User orm xml 文件
  • 学说迁移phar,如何设置类型映射?

    我想在我的非 symfony 项目中使用学说迁移 所以我从https github com doctrine migrations https github com doctrine migrations 我正确配置了所有内容 数据库配置和
  • Symfony2 中的 getEntityManager() 和 getDoctrine()

    这两种说法有什么区别吗 this gt getDoctrine gt getEntityManager gt getRepository this gt getDoctrine gt getRepository 这种差异是否与我遗漏的任何
  • 如何在doctrine2查询生成器where语句中添加运算符

    我试图从一个表中进行选择 其中所选持续时间除以条目持续时间等于没有余数 qb em gt createQueryBuilder gt from AcmeBlogBundle Entry e gt andWhere duration e du
  • Codeigniter 回显 [::1] 而不是 localhost

    我正在使用 CodeIgniter 3 作为 Web 平台 并尝试将语义 UI CSS 导入到我的页面中 我正在使用 CodeIgniter 来做到这一点base url 方法中的hrefCSS 导入的属性 但是 semantic css
  • 根据当前配置,没有映射 Doctrine ORM 实体

    我有一个可疑的问题 我有一组现有的带注释的 Doctrine 实体 它们已在 Symfony2 Doctrine2 项目中成功使用 然而 我目前正在将该项目的一些核心功能隔离到它自己的 Web 框架独立库中 但我似乎无法让这些实体正常运行
  • Symfony 3.4 没有元数据类来处理错误

    我已经安装了 symfony 3 4 和 学说包 php gt 5 5 9 doctrine doctrine bundle 1 8 doctrine doctrine migrations bundle 1 3 doctrine orm
  • 如何通过 SyliusResourceBundle 使用 Sylius 创建新模型

    我找到并成功使用了有关如何覆盖 Sylius 中现有模型的文档 但我无法利用 SyliusResourceBundle 创建一个全新的模型 我猜如果你已经了解 Symfony2 的话这很容易吗 我仍在学习 所以这就是我所拥有的 我缺少什么
  • 使用 Doctrine2 时的多重歧视级别

    我正在使用 Doctrine2 来管理我的模型 如下 有一个抽象概念Content与复合模式Gallery 也是一个抽象概念Media从中Video and Image继承 我的选择是添加鉴别器Content and Media表以便区分G

随机推荐