【问题标题】:Doctrine 2 multiple mapping inheritanceDoctrine 2 多重映射继承
【发布时间】:2012-07-03 10:00:06
【问题描述】:

我为所有具有 GeoLocation 功能的实体定义了一个映射超类:

<?php
namespace Acme\GeoBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperclass
 */
class Placeable {

/**
* @ORM\Column(type="decimal", scale=7)
*/
protected $loc_latitude;

[...]

}
?>

我目前将两个实体映射到这个超类。 但是,我还想将此地理映射提供给我的用户实体,由 FOSUserBundle 提供。问题是这个实体已经从 FOSUserBundle 中包含了 User 模型:

<?php
namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;

/**
 * @ORM\Entity
 */
class User extends BaseUser {}
?>

所以我的问题是:如何将 Placeable 的映射信息提供给我的用户实体?

【问题讨论】:

    标签: inheritance orm symfony doctrine doctrine-orm


    【解决方案1】:

    你应该有一个 Location/Address 对象,继承自 Placeable,并将 Location 与 User 链接起来。

    PS : 这是因为你不能从 PHP 中的多个实体继承

    <?php
    
    class User extends BaseUser
    {
        /**
         * @ORM\OneToOne(targetEntity="Location", inversedBy="user")
         * @ORM\JoinColumn(name="location_id", referencedColumnName="id")
         */
        private $location;
    }
    
    class Location extends Placeable
    {
        /**
         * @ORM\OneToOne(targetEntity="User", mappedBy="location")
         */
        private $user;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多