这是我的第一个问题,所以我希望我能尽其所能。
我有 2 个具有多对多关系的学说实体,效果很好,但我还想在多对多表上添加额外的检查。问题是在 DQL 中我无法找到正确的列。
我想到了 2 种可能性:抽象父类或本机查询。我将粘贴在下面有效的本机查询,但这不是我所希望的,因为我们必须复制和粘贴它。
所以我想建立关系+类型。所以对于这个例子,我还需要检查 type = 'Gallery'
gallery_object (Table)
id、标签、已删除
1、赞助商、0
media_relations (表)
id, mediaid, type, typeId
1, 37, gallery, 1
media_files
id, userid, 文件名, 文件路径, 文件大小, mime_type, 日期, 删除
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;