Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release
#61

[eluser]Hyra[/eluser]
Yep, that would work. Just tried it.

Code:
<?
$comments = $post->get_related('Comments');
        
// Added
for($i=0; $i<count($comments); $i++) {
     $poster_info = $this->users->find($comments[$i]->uid());
     $comments[$i]->UserName = $poster_info->UserName;
}
?&gt;

But I don't know. It's not ideal Smile
Especially not when there's other tables with relationships you want to query too.

Maybe I could do with a chained get_related ..

$comments = $this->post->get_related('Comments')->get_related('Users');

Anyone got a suggestion?
#62

[eluser]flojon[/eluser]
I think that last loop is wrong.. You are using a comment id to find a user? The comment table should probably have a relation to the users table instead. So add belongs_to users in comments and has_many comments in users. Then you can use get_related in the last loop.
#63

[eluser]Hyra[/eluser]
Ah yes. I added $__id_col = "UserID" in the Comments model in my search for the sollution, so uid() worked. Even though working it's indeed wrong. Thanks.

But besides that, I thought of your suggestion too. Add another relationship to the comments model, but you're not allowed as you can't do a double relation.

Code:
class Comments extends IgnitedRecord {

    var $__table = "Comments";
    var $__id_col = "CommentID";
    
    var $__belongs_to = array('table' => 'Posts',  'col' => 'PostID');

    // Adding this would make sense, but you can't re-declare the __belongs_to property
    var $__belongs_to = array('table' => 'Users', 'col' => 'UserID');

    function __construct() {
        parent::IgnitedRecord();
    }

};

EDIT: Just deleted the whole Comments model, and everything is still working. So adding the extra belong_to doesn't seem the thing that would solve it anyway. I might have to look into the Posts model some more instead.
#64

[eluser]flojon[/eluser]
[quote author="m4rw3r" date="1215600994"]@flojon
I realize how useful it is for a behaviour to be able to abort save (and delete, perhaps?), so I will apply your patch (thanks!).
[/quote]

Just be aware that functions using save (and delete) will also need be able to abort if save (or delete) aborts (and then functions which use those functions... ad infinitum)
But I think it will be a nice addition.

I will release my validation behaviour when it's more polished. Right now I have adopted the CI style where you define rules for each field. And add xx_error properties with the validation message.
I would welcome thoughts or comments on using validation in the model. I think it's an interesting way to do it.
#65

[eluser]flojon[/eluser]
This should work:
Code:
var $__belongs_to = array(array('table' => 'Posts',  'col' => 'PostID'), array('table' => 'Users', 'col' => 'UserID'))
#66

[eluser]Hyra[/eluser]
[quote author="flojon" date="1215611885"]This should work:
Code:
var $__belongs_to = array(array('table' => 'Posts',  'col' => 'PostID'), array('table' => 'Users', 'col' => 'UserID'))
[/quote]

Ah, didn't know you could do that.Thanks.

But it doesn't seem to "solve" the problem. I'm still unable to access the username from the $comments object, even though the relations in the comments model are in place.

I print_r'ed the $comments to see if i could spot the usernames in a place (sub array or object from one of the childs) but the usernames don't show up at all.
#67

[eluser]m4rw3r[/eluser]
This should probably work:
Code:
$comments = array();
foreach($post->get_related('Comments') as $comment){
     $user =& $comment->get_related('users'); // "users" here, because you have your model called users
     $comment->username = $user->username;
     $comments[] =& $comment;
}

With
Code:
$this->users->find($comments[$i]->uid());
you asked IR to fetch a user where the user's uid was identical with the comment's uid.

(I guess you wanted
Code:
$this->users->find($comments[$i]->UserID);
, but a foreach is easier to read, according to me).
#68

[eluser]Hyra[/eluser]
Ah! That could work out nicely Smile I'll wrap some of the more repetitive tasks such as that on up in a libray.

I'll give it a twirl. Thanks a lot and keep up the good work.
#69

[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:
&lt;?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:
&lt;?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!
#70

[eluser]m4rw3r[/eluser]
@sophistry

Thanks for the positive feedback!
I've started to reorganize the manual and I'm going to create a quick start guide (probably with your example, among others Tongue), because I realize that it could be hard to understand *everything* by just reading the manual, I'll include a basic "hands on" tutorial or something in Quick start.

I keep forgetting that you cannot extend the loader in PHP 4 (stupid me), so I'll correct that in the new manual.
I'll also add a dedicated Configuration section.

@All

Keep up the good suggestions and comments!




Theme © iAndrew 2016 - Forum software by © MyBB