学说:关于关系的多余(多对多)

时间:2013-02-05 13:13:09

标签: php doctrine-orm many-to-many

这是我的第一个问题所以我希望我能做到应有的一切。

我有2个具有多对多关系的学说实体,并且工作正常,但我还想在多对多表上添加额外的检查。问题是在DQL中我无法进入正确的列。

我想到了两种可能性:抽象父类或本机查询。 我将粘贴下面有效的本机查询,但这不是我希望的,因为我们必须复制并粘贴它。

所以我想建立关系+类型。所以对于这个例子,我还需要检查type ='Gallery'

gallery_object(表)
id,tag,deleted
1,赞助商,0

media_relations(表)
id,mediaid,type,typeId
1,37,画廊,1

media_files
id,userid,filename,filepath,filesize,mime_type,date,deleted
37,4,533882_10151332524797037_1940030593_n_20.jpg,/ resources / upload / www /,82724,image / jpeg,2013-01-25 15:04:46,0

$rsm = new ResultSetMapping;
$rsm->addEntityResult(Helper::getNamespace('Gallery', 'Models/Entities') . "Object", 'o')
    ->addFieldResult('o', 'id', 'id')
    ->addFieldResult('o', 'tag', 'tag')
    ->addFieldResult('o', 'deleted', 'deleted')
    ->addJoinedEntityResult(ltrim(Helper::getNamespace('Media', 'Models/Entities') . "Media", '\\'), 'm', 'o', 'file')
    ->addFieldResult('o', 'file', 'id')
    ->addFieldResult('m', 'mid', 'id')
    ->addFieldResult('m', 'user', 'userid')
    ->addFieldResult('m', 'filename', 'filename')
    ->addFieldResult('m', 'filepath', 'filepath')
    ->addFieldResult('m', 'filesize', 'filesize')
    ->addFieldResult('m', 'mime_type', 'mime_type')
    ->addFieldResult('m', 'date', 'date')
    ->addFieldResult('m', 'mdeleted', 'deleted');

$sql = "SELECT o.id AS oid, m.id as file, o.tag, o.deleted, m.id AS mid, m.userid, m.filename, m.filepath, m.filesize, m.mime_type, m.date, m.deleted as mdeleted
    FROM gallery_object as o
        INNER JOIN media_relations mr ON (mr.typeid = o.id AND mr.type = 'Gallery')
        INNER JOIN media_files m ON (mr.mediaid = m.id)";
$result = $this->_em->createNativeQuery($sql, $rsm)->getResult();

return $result;

实体看起来像

/**
 * Media entity
 *
 * @Entity(repositoryClass = "iTet\Application\Modules\Media\Models\Repositories\Media")
 * @Table(name="media_files")
 * @author Stephen Fenne
 */

class Media
{
/**
 * @Id
 * @Column(type="integer")
 * @GeneratedValue
 * @var int
 */
protected $id;

/**
 * @ManyToOne(targetEntity="iTet\Application\Modules\Core\User\Models\Entities\User")
 * @JoinColumn(name="userId", referencedColumnName="id")
 * @var int
 */
protected $user;

/**
 * @Column(length = 100)
 * @var string
 */
protected $filename;

/**
 * @Column
 * @var string
 */
protected $filepath;

/**
 * @Column(type = "integer")
 * @var int
 */
protected $filesize;

/**
 * @Column
 * @var string
 */
protected $mime_type;

/**
 * @Column(type = "datetime", nullable=true)
 * @var \DateTime 
 */
protected $date;

/**
 * @Column(type = "integer")
 * @var int
 */
protected $deleted = false;

-

/**
 *
 * @Entity(repositoryClass="iTet\Application\Modules\Gallery\Models\Repositories\Object")
 * @Table(name="gallery_object")
 * @author Ward Peeters <ward@coding-tech.com>
 * @package
 */
class Object
{
/** @Id
 * @Column(type="integer")
 * @GeneratedValue
 * @var int */
protected $id;
/** @ManyToMany(targetEntity="iTet\Application\Modules\Media\Models\Entities\Media")
 * @JoinTable(name="media_relations",
 *   joinColumns={@JoinColumn(name="typeid", referencedColumnName="id")},
 *   inverseJoinColumns={@JoinColumn(name="mediaid", referencedColumnName="id")}
 * )
 * @var Media */
protected $file;
/** @Column
 * @var string */
protected $tag;
/** @Column(type="integer")
 * @var bool */
protected $deleted = false;

1 个答案:

答案 0 :(得分:0)

根据我的意见,您的选择如下:

  1. 引入一个实体来表示media_relations表中的条目,以及与其他表的适当关联。
  2. 通过类表继承引入“媒体关系”层次结构(以模拟不同的“类型”)并仅在“图库”类型上进行查询。 Doctrine inheritance documentation.
  3. 在这两种情况下,您都会将基础表暴露给Doctrine,以便您可以使用DQL来获取所需的数据。

    HTH