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

[eluser]dexcell[/eluser]
Hello, i don't know if this is a bug or my mistake

I've try to translate this CI AR into IR

CI AR code:

Code:
$this->db->select('*')
            ->from('users')
            ->join('entries', 'users.id = entries.user_id')
            ->join('qualifiers', 'entries.qualifier_id = qualifiers.id')
            ->where('entries.user_id', $id)
            ->order_by('entries.created', 'desc')
            ->get()->result();

I've translate it to this:

Code:
$this->user->select('*', false)
            ->join('entries', 'users.id = entries.user_id')
            ->join('qualifiers', 'entries.qualifier_id = qualifiers.id')
            ->where('entries.user_id', $id)
            ->order_by('entries.created', 'desc')
            ->find_all();

It only return the one row (the first) instead of many row.

However it works fine if i'm using IR code like this:

Code:
return $this->user->find($id)
            ->related('entries')
            ->from('qualifiers')
            ->select('qualifiers.name AS name')
            ->where('entries.qualifier_id = qualifiers.id')
            ->order_by('entries.created', 'desc')
            ->get();

[eluser]dexcell[/eluser]
Another question,
I have a users table and friends table

Users table:

Quote:id
username

Friends table:
Quote:user_id
friend_id (also referencing to id in users table)

My question is:
Ex: I want list of username A friends username.

or ex: I want list of user id 1 friends username.

Is it possible to accomplish this using one query in IR?
starting from $this->user->

Thank you

PS: Wow so the 1.0 release will come out soon, can't wait for it Smile Thanks for your hardwork m4rw3r, really appreciate it.

[eluser]m4rw3r[/eluser]
Here is the answer to the first of your two questions:

The "error" you encountered with the second code block is a feature. IgnitedRecord does only create a single object for each of the unique primary keys in the result.

Example:
Code:
SELECT * FROM groups LEFT JOIN users ON groups.user_id = users.id

id ... user_id users.id users.name
1  ... 1       1        foo
1  ... 2       2        bar
1  ... 3       3        john doe
2  ... 4       4        someone
Will translate to two objects, one for the first row with id 1 and user_id 1, and one for the fourth row with id 2 and user_id 4.

If you use join_related() instead of a normal join, the user data will be available in objects stored in a property of the group record.
This is because join_related() tracks what have been joined and then dbobj2orm() does the rest (the manual joins are also trackable, but it must be done manually).

This will return all users with an entries property containing the entry records with qualifiers data (and entry data, of course):
Code:
$this->user->join_related('entries')
            ->join('qualifiers', 'entries.qualifier_id = qualifiers.id')
            ->order_by('entries.created', 'desc')
            ->find_all();

[eluser]m4rw3r[/eluser]
Your second question:

This should work:
Code:
class user extends IgnitedRecord{
    var $habtm = array('name'  => 'friends',
                       'table' => 'users',
                       'join_table' => 'friends', // default is users_users :)
                       'fk'    => 'user_id',
                       'r_fk'  => 'friend_id');
}
Then in the controller:
Code:
$this->user->find_by('username', 'A')->related('friends')->get();
and your second example:
Code:
$this->user->find(1)->related('friends')->get();
The relation above is a single way relation, so if two users should be friends with eachother, you have to do this:
Code:
$user1->add('friends', $user2);
$user2->add('friends', $user1);

[eluser]longlostcousin[/eluser]
hi m4rw3r,

would it be possible for you to give a quick example of how the form generation part of the validation behaviour works? i know it's still a work in progress but i'd love to try and start using it. thanks!

mark

[eluser]dexcell[/eluser]
[quote author="m4rw3r" date="1225405883"]Here is the answer to the first of your two questions:

The "error" you encountered with the second code block is a feature. IgnitedRecord does only create a single object for each of the unique primary keys in the result.

Example:
Code:
SELECT * FROM groups LEFT JOIN users ON groups.user_id = users.id

id ... user_id users.id users.name
1  ... 1       1        foo
1  ... 2       2        bar
1  ... 3       3        john doe
2  ... 4       4        someone
Will translate to two objects, one for the first row with id 1 and user_id 1, and one for the fourth row with id 2 and user_id 4.

If you use join_related() instead of a normal join, the user data will be available in objects stored in a property of the group record.
This is because join_related() tracks what have been joined and then dbobj2orm() does the rest (the manual joins are also trackable, but it must be done manually).

This will return all users with an entries property containing the entry records with qualifiers data (and entry data, of course):
Code:
$this->user->join_related('entries')
            ->join('qualifiers', 'entries.qualifier_id = qualifiers.id')
            ->order_by('entries.created', 'desc')
            ->find_all();
[/quote]

Thank you for your explanation and code, but unfortunately the code doesn't work as expected, maybe because the relation of user and entries? user can have many entries(like post in blog) so join_related cannot be used?

The error showing when i try to access the field
for example: using $entry->text (text is one of field in entries table).

The error showed up is
Code:
Undefined property: IR_record::$text

[eluser]dexcell[/eluser]
[quote author="m4rw3r" date="1225406407"]Your second question:

This should work:
Code:
class user extends IgnitedRecord{
    var $habtm = array('name'  => 'friends',
                       'table' => 'users',
                       'join_table' => 'friends', // default is users_users :)
                       'fk'    => 'user_id',
                       'r_fk'  => 'friend_id');
}
Then in the controller:
Code:
$this->user->find_by('username', 'A')->related('friends')->get();
and your second example:
Code:
$this->user->find(1)->related('friends')->get();
The relation above is a single way relation, so if two users should be friends with eachother, you have to do this:
Code:
$user1->add('friends', $user2);
$user2->add('friends', $user1);
[/quote]

Thanks, I'm getting the idea of what you writing.
But when i tried to use the code:

Code:
$this->user->find(2)->related('friends')->get();

Got an SQL error show up
Quote:A Database Error Occurred

Error Number: 1066

Not unique table/alias: 'friends'

SELECT `friends`.* FROM `users` AS `friends` JOIN `friends` ON friends.friend_id = friends.id WHERE `friends`.`user_id` = '2'

Any idea?
Thank you

[eluser]m4rw3r[/eluser]
@longlostcousin:
The form generation feature is currently subject to change, and I need to make a view, internationalize it and revise the API. Everything to avoid messy code when using it.
I'll post something when I have got the internationalization working.

@dexcell:
The first error: can you show me some code?
Because the beautiful thing with the new dbobj2ORM() method is that it also handles JOINs to Has Many and HABTM relations, the related records are stored in an array in a property of the "main" object(s).

Try call:
Code:
idump($result); // $result is the return from the code I posted
Then you'll see the structure of the data.

idump() is a (new) dump function in IR which helps with dumping the data of IR_record objects, omitting the "internal" properties so anyone dumping an IR_record can avoid a Wall Of Text (anyone used print_r($record); Tongue ).
(you can also call $record->dump(); to dump a single object)

The second error occurs because of the naming of the join table. It is identical to the relation name, and hence it collides (call it friend_rel or something instead).

[eluser]dexcell[/eluser]
Thanks for reply m4rw3r.

Didn't notice it's already rev 204 in svn.
idump function is really helpful i think Big Grin

Now, after i've seen the structure using idump, i can say it returns correctly Smile

I wanna ask is how do i get the entry field using $result, and there is still no qualifiers field information from qualifiers table that i can extract even tough there is a join between entries and qualifiers.

The code
Code:
$id = 2;
$result = $this->user->join_related('entries')
            ->join('qualifiers', 'entries.qualifier_id = qualifiers.id')
            ->where('entries.user_id', $id)
            ->order_by('entries.created', 'desc')            
            ->find_all();

Here is the idump (trimmed unused field).
Quote:Array
{
[s:1:"2";] => IR_record linked to users(2), model: "user"
{
[entries* => Array
{
[0] => IR_record linked to entries(5), model: "entry"
{
[id] => string(1) "5"
[qualifier_id] => string(2) "15"
[user_id] => string(1) "2"
[text] => string(6) "hiking" <-how to get this info? using $result in code?
[created] => string(19) "2008-10-21 23:32:36"
}
[1] => IR_record linked to entries(4), model: "entry"
{
[id] => string(1) "4"
[group_id] => string(1) "0"
[qualifier_id] => string(1) "5"
[user_id] => string(1) "2"
[text] => string(5) "happy"
[created] => string(19) "2008-10-21 23:09:51"
}

}
[id] => string(1) "2"
[username] => string(6) "google"
[created] => string(19) "2008-10-20 03:18:28"
[modified] => string(19) "2008-10-27 21:12:05"
}
}

Thanks Smile

And, the friends problem is already solved thanks to you Smile

[eluser]dexcell[/eluser]
[quote author="m4rw3r" date="1225405883"]Here is the answer to the first of your two questions:

The "error" you encountered with the second code block is a feature. IgnitedRecord does only create a single object for each of the unique primary keys in the result.
[/quote]

Just wondering, Is there a way to turn off that feature? so it will perform like CI AR join Tongue




Theme © iAndrew 2016 - Forum software by © MyBB