简体   繁体   English

在Codeigniter中使用Datamapper ORM在同一对象上进行多对多和多对多

[英]Many to one and Many to many on same objects using datamapper ORM in Codeigniter

I have an application that requires: 我的应用程序需要:

user owns many projects. 用户拥有许多项目。 project has one owner. 项目只有一位所有者。

user works on many projects. 用户从事许多项目。 projects have many users. 项目有很多用户。

so I have 3 tables, users, projects_users, projects. 所以我有3个表,用户,projects_users,项目。 The relationships are: 关系是:

one user (owner) --- many projects (created_by) 一个用户(所有者)---许多项目(created_by)

many users (id) ---- via projects_users (user_id, project_id) ---- many projects (id). 许多用户(id)----通过projects_users(user_id,project_id)----许多项目(id)。

In Codeigniter I've set up the following relationships in the datamapper models: 在Codeigniter中,我在datamapper模型中设置了以下关系:

Class Project extends DataMapper {

var $has_one = array(
    'created_by' => array(
        'class' => 'user',
        'other_field' => 'owns'
    )
);
var $has_many = array('user' => array(
        'class' => 'user',
        'other_field' => 'project',
        'join_table' => 'projects_users'));

and... 和...

    class User extends DataMapper {

var $has_many = array(
    'project' => array(
        'class' => 'project',
        'other_field' => 'user',
        'join_table' => 'projects_users'
    ),
    'owns' => array(
        'class' => 'project',
        'other_field' => 'created_by'
    )
);

This doesn't seem to work however and I get a recursive error. 但是,这似乎不起作用,并且出现了递归错误。 What is the correct way to represent this relationship in datamapper? 在datamapper中表示这种关系的正确方法是什么?

Take a look at Multiple Relationships to the Same Model on the doc page: http://datamapper.wanwizard.eu/pages/advancedrelations.html 在文档页面上查看与同一模型的多重关系http : //datamapper.wanwizard.eu/pages/advancedrelations.html

I'm guessing something like this: 我猜是这样的:

class Project extends DataMapper {
    $has_one = array(
        'owner' => array(
            'class' => 'user',
            'other_field' => 'owned_project'
        )
    );
    $has_many = array(
        'user' => array(
            'class' => 'user',
            'other_field' => 'user_project'
        )
    );
}

class User extends DataMapper {
    $has_many = array(
        'owned_project' => array(
            'class' => 'project',
            'other_field' => 'owner'
        ),
        'user_project' => array(
            'class' => 'project',
            'other_field' => 'user'
        )
    );
}

and you would access like this: 您将像这样访问:

$project = new Project(1); // where "1" is the ID of the project
$owner = $project->owner->get();
$project_users = $project->user->get();

// -------------------------------

$me = new User(1); // where "1" is the ID of the user
$my_projects = $me->owned_project->get();

UPDATE UPDATE

You will need to update your projects_users join table to this: 您将需要将您的projects_users连接表更新为:

projects_users
--------------
user_id
owner_id
owned_project_id
user_project_id

Note that these all match the "keys" that you declared in your $has_one and $has_many arrays. 请注意,这些都与您在$has_one$has_many数组中声明的“键”匹配。 You will not need to update the users or projects tables (the only requirement is that each of them have a primary key field named "id") . 您将不需要更新users表或projects表(唯一的要求是每个users表或projects表都有一个名为“ id”的主键字段)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM