用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1818|回复: 0
收起左侧

[讨论/交流] DataMapper ORM 中大多数开发者都不明白的问题

[复制链接]
发表于 2012-9-2 09:53:07 | 显示全部楼层 |阅读模式
本帖最后由 Mr_yinl 于 2012-9-3 10:06 编辑

问题汇总:

      1. DataMapper ORM 根据手册上提供的资料,似乎要做关联查询时 如  has_one, has_many 都要新增一个关联关系表也就是中 间表个人觉得不是很理想,但看到有个别网友 也出现了这个问题,但似乎也得到了解决,说是配置不当的原因,但我真的没有明白要怎么做才可以不用中间表。希望给出解答,谢谢!

      2. 对于这些属性我的理解你看下是否正确,谢谢

// 这是被关联的 模型代码,之所以放一起是希望你能看明白
// 文件名:author_model.php
// 示例代码:
//class Author_model extends DataMapper {
//
// public $table = 'authors';
//
//    var $has_many = array(
//        'book' => array(   //
//            'class' => 'book_model',   
//            'other_field' => 'author',  
//            'join_self_as' => 'author',  
//            'join_other_as' => 'book',  
//            'join_table' => 'authors_books'
//     ),
//    );
//}
// 中间表  authors_books    SQL
//Create Table
//
//CREATE TABLE `authors_books` (
//  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
//  `author_id` int(10) unsigned NOT NULL,
//  `book_id` int(10) DEFAULT NULL,
//  PRIMARY KEY (`id`,`author_id`)
//) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

class Book_model extends DataMapper {

var $table = 'books';

    var $has_many = array(
        'author' => array(   
            'class' => 'author_model', //要关联的模型的 类名
            'other_field' => 'book ', //要关联的模型的   关联下标  "book"  即 //    var $has_many = array(
                  //       'book' => array(   //
                  //            'class' => 'book_model',
                  //    ),
                  //);
            'join_self_as' => 'book', // 中间表即关联关系表中当前 model 表的关联字段
            'join_other_as' => 'author',  //中间表即关联关系表中被关联 author_model 表中 的关联字段
            'join_table' => 'authors_books' //中间表  "authors_books"  即关联关系表
     ) // name of the join table that will link both Author and Book together
    );
}

3. join_self_as 和 join_other_as  总是 $field . '_id' 即 它定义 关联字段必须是  ***_id  如果我的关联字段是 aothorid

那么 sql 就会出错  left join xxx on 里就会变成 aaa.id=B.aothorid_id   这样就太不灵活,不知道是不是配置的问题

就这些问题,太谢谢了

本版积分规则