在Symfony2中多使用JoinColumns学说注解? [英] Multiple JoinColumns in Symfony2 using Doctrine annotations?

查看:148
本文介绍了在Symfony2中多使用JoinColumns学说注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的问题是:

路由带属性的 OBJECTID 的objectType OBJECTID 是一个int,而的objectType 是一个字符串。这样做的原因是为了让同桌的不同类型的路由的保存数据。例如,对于产品的路由品牌 。因此,组合的的objectType OBJECTID 是我JoinColumn。

我如何创建Doctrine2这样的双向关系?我看着继承关系,但没有概念似乎正是我要找的。

我可以在数据库中创建了一些看法,只是有几个不同的路由实体,但这似乎并没有最好的路线。

下面是我的实体产品品牌

../实体/ Department.php

 使用Doctrine \\ ORM \\映射作为ORM;/ **
 * @ORM \\实体()
 * @ORM \\表(名称=部门)
 * /
系类实现DescribableInterface
{
    / **
     * @ORM \\标识
     * @ORM \\列(名称=ID,类型=​​整数,长度= 11)
     * @ORM \\ GeneratedValue(策略=AUTO)
     * /
    私人的$ id;    / **
     * @ORM \\列(名称=身份,键入=字符串,长度= 1)
     * /
    私人$地位;    / **
     * @ORM \\列(名称=名称中输入=字符串,长度= 255)
     * /
    私人$名称;
...

../实体/ Product.php

 使用Doctrine \\ ORM \\映射作为ORM;/ **
 * @ORM \\实体
 * @ORM \\表(名称=产品)
 * /
类产品实现DescribableInterface
{
    / **
     * @ORM \\标识
     * @ORM \\列(名称=ID,类型=​​整数,长度= 11)
     * @ORM \\ GeneratedValue(策略=AUTO)
     * /
    私人的$ id;    / **
     * @ORM \\列(名称=身份,键入=字符串,长度= 1)
     * /
    私人$地位;    / **
     * @ORM \\列(名称=product_ code,类型=​​字符串,长度= 100,可为空=真)
     * /
    私人$产品code ='';    / **
     * @ORM \\列(名称=名称中输入=字符串,长度= 255)
     * /
    私人$名称;
...

../实体/ Brand.php

 使用Doctrine \\ ORM \\映射作为ORM;/ **
 * @ORM \\实体
 * @ORM \\表(名称=品牌)
 * /
一流品牌
{
    / **
     * @ORM \\标识
     * @ORM \\列(名称=ID,类型=​​整数,长度= 11)
     * @ORM \\ GeneratedValue(策略=AUTO)
     * /
    私人的$ id;    / **
     * @ORM \\列(名称=身份,键入=字符串,长度= 1)
     * /
    私人$状态='A';    / **
     * @ORM \\列(名称=名称中输入=字符串,长度= 255)
     * /
    私人$名称;
...

每个产品,品牌和部门都有一个由对象类型 OBJECT_ID 其中对象类型简直就是部门产品品牌 OBJECT_ID 是相应的产品,品牌或部门的唯一ID。

../实体/ Routing.php

 使用Doctrine \\ ORM \\映射作为ORM;/ **
 * @ORM \\实体
 * @ORM \\表(名称=路由)
 * /
级路由
{
    / **
     * @ORM \\标识
     * @ORM \\列(名称=ID,类型=​​整数,长度= 11)
     * @ORM \\ GeneratedValue(策略=AUTO)
     * /
    私人的$ id;    / **
     * @ORM \\列(名称=OBJECT_ID,键入=整数,长度= 11)
     * /
    私人$ OBJECTID;    / **
     * @ORM \\列(名称=对象类型,类型=​​字符串,长度为100)
     * /
    私人$的objectType;    / **
     * @ORM \\列(名称=URL,键入=文本)
     * /
    私人$网址;
...

我真的很挣扎是怎样安装的关系,所以各部门,产品和品牌能够从单一路由访问他们的网址。

我曾尝试加入到 $ OBJECTID 的关系,但它似乎不喜欢这样。是否可以设置它这样?

什么,我基本上是试图实现的是获取数据对象,并要得到一个对象的URL的能力,例如:

  $有关部门= $的EM> getRepository(AdamStaceySiteBundle:系) - >的findAll();
的foreach($部门为$部)
{
   回声'&下; A HREF ='$部 - 方式>使用getURL()。>'。$部 - > getMenuTitle()'&所述; /一个取代;
}

谁能帮助?


解决方案

在进一步的研究,我发现在认识一个人(德克Olbertz)谁了同样的问题。

谷歌网上论坛:多JoinColumns

信息可以在这里找到?

我现在已经实现了这个,我会解释我是如何做到柜面它可以帮助其他人。

在回答我的问题是使用单表继承的。

我需要做的第一件事就是更新为使用单表继承了路由实体:

../实体/ Routing.php

  / **
 * @ORM \\实体
 * @ORM \\ InheritanceType(SINGLE_TABLE)
 * @ORM \\ DiscriminatorColumn(NAME =对象类型,类型=​​字符串)
 * @ORM \\ DiscriminatorMap({产品=ProductRouting,部门=DepartmentRouting,品牌=BrandRouting})
 * @ORM \\表(名称=路由)
 * /
级路由
{
    / **
     * @ORM \\标识
     * @ORM \\列(名称=ID,类型=​​整数,长度= 11)
     * @ORM \\ GeneratedValue(策略=AUTO)
     * /
    私人的$ id;
...

DiscriminatorColumn 让我来指定哪些列将用于链接,在这种情况下是对象类型域。

DiscriminatorMap 让我来指定哪些对象类型将与哪些实体联系起来。

这些实体则必须创建扩展路由实体。

../实体/ ProductRouting.php

  / **
 * @ORM \\实体
 * /
类ProductRouting扩展路由
{
    / **
     * @ORM \\多对一(targetEntity =产品)
     * @ORM \\ JoinColumn(NAME =OBJECT_ID,referencedColumnName =ID)
     * /
    保护$产品;
...

../实体/ DepartmentRouting.php

  / **
 * @ORM \\实体
 * /
类DepartmentRouting扩展路由
{
    / **
     * @ORM \\多对一(targetEntity =部门)
     * @ORM \\ JoinColumn(NAME =OBJECT_ID,referencedColumnName =ID)
     * /
    保护$部;
...

../实体/ BrandRouting.php

  / **
 * @ORM \\实体
 * /
类BrandRouting扩展路由
{
    / **
     * @ORM \\多对一(targetEntity =品牌)
     * @ORM \\ JoinColumn(NAME =OBJECT_ID,referencedColumnName =ID)
     * /
    保护$品牌;
...

然后在每个产品品牌实体我需要添加新的 $路线

../实体/ Product.php

  ...
一流的产品
{
    ...
    / **
     * @ORM \\一对多(targetEntity =ProductRouting的mappedBy =产品,级联= {所有})
     * /
    私人$路线;
...

../实体/ Department.php

  ...
科类
{
    ...
    / **
     * @ORM \\一对多(targetEntity =DepartmentRouting的mappedBy =部门,级联= {所有})
     * /
    私人$路线;
...

../实体/ Brand.php

  ...
一流品牌
{
    ...
    / **
     * @ORM \\一对多(targetEntity =BrandRouting的mappedBy =品牌,级联= {所有})
     * /
    私人$路线;
...

希望帮助...

Here is the problem:

Class Routing with attributes objectId and objectType. objectId is an int, and objectType is a string. The reason for this was to allow the same table to hold data for different kind of routings. For instance for the routing of Products, Department and Brand. So, the combination of the objectType and the objectId is my JoinColumn.

How do I create such a bidirectional relationship with Doctrine2? I looked at inherited relationships, but none of the concepts seemed to be what I'm looking for.

I could create some views in the database and just have a couple of different Routing entities, but this does not seem the best route.

Here are my entities Department, Product and Brand.

../Entity/Department.php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="departments")
 */
class Department implements DescribableInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", length=11)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

../Entity/Product.php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="products")
 */
class Product implements DescribableInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", length=11)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\Column(name="product_code", type="string", length=100, nullable=true)
     */
    private $productCode = '';

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

../Entity/Brand.php

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\Column(name="status", type="string", length=1)
     */
    private $status = 'a';

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

Each product, brand and department has its own URL that is held in the routing table set by the object_type and object_id where the object_type is simply department, product or brand and the object_id is the unique id of the corresponding product, brand or department.

../Entity/Routing.php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="routing")
 */
class Routing
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", length=11)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;    

    /**
     * @ORM\Column(name="object_id", type="integer", length=11)
     */
    private $objectId;

    /**
     * @ORM\Column(name="object_type", type="string", length=100)
     */
    private $objectType;

    /**
     * @ORM\Column(name="url", type="text")
     */
    private $url;
...

What I am really struggling with is how do I setup the relationship, so the departments, products and brands can access their URL from the single routing entity.

I have tried adding the relationships to the $objectId, but it doesn't seem to like that. Is it possible to set this up like this?

What I am basically trying to achieve is to get the data object and have the ability to get the URL of an object, for example:

$departments = $em->getRepository("AdamStaceySiteBundle:Department")->findAll();
foreach ($departments as $department)
{
   echo '<a href="'.$department->getUrl().'">'.$department->getMenuTitle().'</a>;
}

Can anyone help?

解决方案

After further researching I found a man (Dirk Olbertz) in the know who had the same problem.

Information can be found at: Google Groups: Multiple JoinColumns?

I have now implemented this and I will explain how I did it incase it might help anyone else.

The answer to my problem was the use of single table inheritance.

The first thing I needed to do was update the routing entity to use single table inheritance:

../Entity/Routing.php

/**
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="object_type", type="string")
 * @ORM\DiscriminatorMap({"product" = "ProductRouting", "department" = "DepartmentRouting", "brand" = "BrandRouting"})
 * @ORM\Table(name="routing")
 */
class Routing
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer", length=11)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
...

The DiscriminatorColumn allowed me to specify what column would be used to link, which in this case was the object_type field.

The DiscriminatorMap allowed me to specify what object_type will link with what entities.

These entities then had to be created that extended the Routing entity.

../Entity/ProductRouting.php

/**
 * @ORM\Entity
 */
class ProductRouting extends Routing
{
    /**
     * @ORM\ManyToOne(targetEntity="Product")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
     */
    protected $product;
...

../Entity/DepartmentRouting.php

/**
 * @ORM\Entity
 */
class DepartmentRouting extends Routing
{
    /**
     * @ORM\ManyToOne(targetEntity="Department")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
     */
    protected $department;
...

../Entity/BrandRouting.php

/**
 * @ORM\Entity
 */
class BrandRouting extends Routing
{
    /**
     * @ORM\ManyToOne(targetEntity="Brand")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
     */
    protected $brand;
...

Then in each of the Product, Department and Brand entities I needed to add the new $routings.

../Entity/Product.php

...
class Product
{
    ...
    /**
     * @ORM\OneToMany(targetEntity="ProductRouting", mappedBy="product", cascade={"all"})
     */
    private $routings;
...

../Entity/Department.php

...
class Department
{
    ...
    /**
     * @ORM\OneToMany(targetEntity="DepartmentRouting", mappedBy="department", cascade={"all"})
     */
    private $routings;
...

../Entity/Brand.php

...
class Brand
{
    ...
    /**
     * @ORM\OneToMany(targetEntity="BrandRouting", mappedBy="brand", cascade={"all"})
     */
    private $routings;
...

Hope that helps...

这篇关于在Symfony2中多使用JoinColumns学说注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆