[eluser]sophistry[/eluser]
@m4rw3r - nice work here.
esp. thx for php4 support. i changed = new to =& new as suggested. that worked fine, but then i stumbled on the use of MY_Loader.php which apparently needs a small hack to the CI_Loader to work in PHP4. i don't like hacking the core files so i just load the ignitedrecord.php model manually as specified in the manual. maybe there should be a note in the docs saying "If you are on PHP4 then just load the ignitedrecord.php model in the normal CI way."
i tested this morning and after a bit of fiddling, i got it up and running in an hour or so.
MAJOR kudos on the documentation effort! it takes a huge push to code a brilliant ORM system AND get full documentation together. i found the documentation thorough but a little disorganized because there are so many options and refinements and overrides and they are all detailed inline. i wonder if there is a "simplest path" quick start way of presenting this?
for instance, you could have a section that says "to make ORM possible in your app with the least code use the standard configurations/defaults." so, in the manual you need to enumerate the Ignited Record db schema assumptions upfront ("use plural table names and use the singular names for your models", "must have id field in every table", "this is the foreign key name pattern reltable_id", "the join pattern assumes alphabetical order for tablename and relationship".
also, i suggest a sample basic db dump to let people get the hang of this with a demo setup. a demo controller and a full example of a model would be nice (i put my code below for your review).
The most basic SQL setup to test HABTM:
Code:
CREATE TABLE `groups` (
`id` int(11) NOT NULL auto_increment,
`name` text,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
INSERT INTO `groups` VALUES (1, 'cool');
INSERT INTO `groups` VALUES (2, 'not cool');
CREATE TABLE `groups_users` (
`id` int(11) NOT NULL auto_increment,
`groups_id` int(11) default NULL,
`users_id` int(11) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
INSERT INTO `groups_users` VALUES (1, 1, 1);
INSERT INTO `groups_users` VALUES (2, 1, 2);
INSERT INTO `groups_users` VALUES (3, 2, 3);
INSERT INTO `groups_users` VALUES (4, 2, 4);
INSERT INTO `groups_users` VALUES (5, 2, 1);
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` text,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
INSERT INTO `users` VALUES (1, 'me');
INSERT INTO `users` VALUES (2, 'you');
INSERT INTO `users` VALUES (3, 'another guy');
INSERT INTO `users` VALUES (4, 'a fourth');
the model with the single habtm relationship:
Code:
<?php
class User extends IgnitedRecord{
var $__has_and_belongs_to_many = 'groups';
function User(){
parent::IgnitedRecord();
}
}
/* End of file user.php */
/* Location: ./system/application/models/user.php */
and the controller with test code:
Code:
<?php
class test_ir extends Controller {
function test_ir()
{
parent::Controller();
// you can't take the MY_Loader.php shortcut in PHP4
// so, load manually to you avoid hacking CI_Loader
// see forum thread: http://ellislab.com/forums/viewthread/83614/
$this->load->model('ignitedrecord/ignitedrecord');
$this->load->model('user');
}
function index()
{
$rec1 = $this->user->find(1);
print_r('user with id #1: '.$rec1->name.'<hr>');
echo 'user '. $rec1->name .' belongs to groups:<br>';
$related_recs = $rec1->get_related('groups');
foreach($related_recs as $r)
{
print_r($r->name.'<br>');
}
echo '<hr> all users:<br>';
$recs = $this->user->find_all();
foreach($recs as $r)
{
print_r($r->name.'<br>');
}
}
}
/* End of file test_ir.php */
/* Location: ./system/application/controllers/test_ir.php */
finally, two small things... load_rel is an alias, shouldn't get_rel be an alias too? and, there is a small typo in the manual: "add_relaionship" should be "add_relationship".
cheers!