DataMapper ORM问题中的多个M2M [英] Multiple M2M in DataMapper ORM issue

查看:118
本文介绍了DataMapper ORM问题中的多个M2M的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在CodeIgniter中为我的DataMapper ORM库构建一个小的核心块,以控制用户对DataMapper对象本身的访问/编辑权限(CRUD).为此,我想将DataMapper EntityUserroleUserright链接在一起.

I am building a small core piece for my DataMapper ORM library in CodeIgniter to control user's access/edit rights (CRUD) over DataMapper objects itself. For this I want to link a DataMapper Entity, Userrole and Userright together.

文档在此处 http://datamapper.wanwizard.eu/pages/advancedrelations.html (在标题下; 多表关系).

The documentation is here http://datamapper.wanwizard.eu/pages/advancedrelations.html (Under head; Multi-table Relationships).

因此,如果我手动填写联接表,则可以毫无问题地获取值.唯一的问题是保存关系.

So if I fill in the join table by hand I can just get the values without any problems. The only problem is saving the relationship.

这是我的模型规则

用户角色

var $has_many = array(
    'userright' => array(
        'join_table' => 'dm_entities_userrights_userroles',
    ),
    'dm_entity' => array(
        'join_table' => 'dm_entities_userrights_userroles',
    ),
);

用户权限

var $has_many = array(
    'dm_entity' => array(
        'join_table' => 'dm_entities_userrights_userroles',
    ),
    'userrole' => array(
        'join_table' => 'dm_entities_userrights_userroles',
    ),
);

Dm_entity

var $has_many = array(
    'userrole' => array(
        'join_table' => 'dm_entities_userrights_userroles',
    ),
    'userright' => array(
        'join_table' => 'dm_entities_userrights_userroles',
    ),
);

奇怪的是,我连续两次声明了该表,但如果不这样做,那似乎就行不通了.

It seems odd I declared the table twice constantly, but it seems not to work if I don't.

所以这是我从Userright;

$userrole = new Userrole;
$userrole->get_where(array('name' => 'Administrator'));

$dm_entity = new Dm_entity;
$dm_entity->get_where(array('name' => 'User'));     

$userrights = new Userright;
$userrights->where_related($userrole);
$userrights->where_related($dm_entity)->get();

$output = '';

foreach ($userrights as $userright) {
    $output .= '<i><b>'.$userright->name.'</b></i> and ';
}

$output = substr($output, 0, -5);

echo '<b>'.$userrole->name.'</b> has the right to '.$output.' the DataMapper entity <b>'.$dm_entity->name.'</b>.'.br();

因此,此方法也没有任何问题.但现在最省钱的是:

So this works without any problems either. But now the saving part:

$new_userright = new Userright;
$new_userright->get_where(array('name' => 'Update'));

$new_userright->save(array($userrole, $dm_entity));

这会在dm_entities_userrights_userroles表中产生两个条目,其中1个条目的dm_entity_id为空,另一个条目的userrole_id为空.

Which results in two entries in the dm_entities_userrights_userroles table where 1 entry has dm_entity_id empty and the other the userrole_id empty.

我希望避免为连接表创建单独的模型来解决此问题.

I hope to avoid making a separate Model for the join table to solve this.

有人知道我怎样才能使它起作用,以便它使一个正确的条目而不是两个分散的条目吗?

Does anyone know how I can get this to work so it makes one correct entry, instead of two scattered ones?

推荐答案

这是行不通的(如您所知).

This is not going to work (as you have noticed).

关系是两个模型之间的关系,对于每个多对多关系,您需要一个单独的联接表.

A relation is between two models, and for each Many to Many relation you need a separate join table.

尽管从技术上讲,您可以创建具有10个FK到10个不同模型的联接表,但是Datamapper不会意识到这一点,并且会针对每个关系将表视为不同,从而导致出现重复项.

Although technically you can create a join table with 10 FK's to 10 different models, Datamapper will not be aware of that, and will treat the table as different for every relation, causing duplicates to appear.

实现此目的的唯一方法是,也为联接表定义一个模型,并与其他每个模型建立一对多的关系.然后,您可以使用该模型通过分配或更新FK值来手动添加关系,而仍然使用多对多关系进行检索和更新.

The only way to make this work, is to define a model for the join table too, and give that one-to-many relations to each of the other models. You can then use that model to manually add relations by assigning or updating the FK values, and still use the many-to-many relations for retrieval and update.

这篇关于DataMapper ORM问题中的多个M2M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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