关于 DataMapper ORM 中大多数开发者都不明白的问题
本帖最后由 Mr_yinl 于 2012-9-3 09:55 编辑问题汇总:
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 这样就太不灵活,不知道是不是配置的问题
就这些问题,太谢谢了
手册里 other_field 是要关联其他表里你设置的has_many字段,而不是自己。
你的 Author_model 是没问题的,但是 Book_model 是把 other_field 写错了,应该写 Author_model 里的has_many字段即 book。 实在不好意思,这个是我那天帖的时候没注意,我源码里是这样写的是 book没错 1 .还有,关联查询 不要中间表,怎么可以实现?
2. join_self_as 和 join_other_as总是 $field . '_id' 即 它定义 关联字段必须是***_id如果我的关联字段是 aothorid
那么 sql 就会出错left join xxx on 里就会变成 aaa.id=B.aothorid_id 这样就太不灵活, 1 .还有,关联查询 不要中间表,怎么可以实现?
2. join_self_as 和 join_other_as总是 $field . '_id' 即 它定义 关联字段必须是***_id如果我的关联字段是 aothorid
那么 sql 就会出错left join xxx on 里就会变成 aaa.id=B.aothorid_id 这样就太不灵活, orm用着却是非常爽,适合较大中型网站。我用了很长时间,因为懒,现在用AR
页:
[1]