CodeIgniter Forums
ActiveRecord for CodeIgniter: Rails-style model interactions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: ActiveRecord for CodeIgniter: Rails-style model interactions (/thread-3261.html)

Pages: 1 2 3 4 5 6 7 8 9 10 11 12


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-20-2007

[eluser]Matthew Pennell[/eluser]
Following on from this thread, I have now got my version of ActiveRecord (based on RoR's implementation) to a point where I'd like to get some more pairs of eyes on it.

I've written up the class and provided a download here:

ActiveRecord Class on the wiki

I'm using it at the moment on a project in development, and it seems to be working fine - no noticeable impact on performance or query speed, and it makes developing a lot faster without having to write CRUD functions for every single model.

In summary, it means that you can do things like this without having to write all the database interaction code yourself:

Code:
$me = $this->Person->find_by_first_name('Buddy');
$me->fetch_related_pages();

$new_page = new Page();
$new_page->title = 'My Title';
$new_page->content = 'Blah blah blah';
$new_page->save();

$me->create_relationship($new_page);

Note that it's PHP5 only.


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-20-2007

[eluser]alexsancho[/eluser]
Really nice addition, but i'm still looking for and update and update_by method, the class only contains methods to create new records, but how you update and existing one?

Thanks again for your contribution


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-20-2007

[eluser]Matthew Pennell[/eluser]
[quote author="alexsancho" date="1190316776"]Really nice addition, but i'm still looking for and update and update_by method, the class only contains methods to create new records, but how you update and existing one?[/quote]
They're on my development machine at home - I'll update the class and the wiki when I get in to add the update method.


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-20-2007

[eluser]Michael Wales[/eluser]
If you want to work on merging this with Fire (I saw your reply in regards to this) feel free to do so. The code is publicly available and you are more than welcome to take it over (just give me a credit line in your readme).


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-20-2007

[eluser]Matthew Pennell[/eluser]
[quote author="Buddy Bradley" date="1190317210"]They're on my development machine at home - I'll update the class and the wiki when I get in to add the update method.[/quote]
I've added the basic update() method to the downloadable code and the wiki article now. Smile


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-21-2007

[eluser]gunter[/eluser]
Hi!

and how can I use the update() ??

like this???

Code:
$me = $this->Person->get_by_id($id);
$me->fetch_related_pages();


$me->title = 'New Title';
$me->content = 'Blah blah blah';
$me->save();
edit: I forget to say wow! and thanks!!


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-21-2007

[eluser]Matthew Pennell[/eluser]
[quote author="gunter" date="1190390055"]and how can I use the update() ??[/quote]
Like this:

Code:
$me = $this->Person->get_by_id($id);
$me->title = 'New Title';
$me->content = 'Blah blah blah';
$me->update();



ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-21-2007

[eluser]danfreak[/eluser]
Thanks for this nice library Buddy!

I'll check it out!

Dan


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-21-2007

[eluser]Michael Wales[/eluser]
I've been looking over this library since it was released - this is looking really, really, nice.

Any chance of getting some better relationships involved? The relationships table you are employing (similar to RoR's HABTM relationships) is a bit excessive for just a belongs_to or has_many relationship.

For instance:

Table: pages
id
user_id
title
body

Table: users
id
username
email

Crating a pages_users table is a bit excessive and increases the number of calls made to the database, when a relationship could simply be formed between pages.user_id and users.id columns.

I've yet to actually play with this library, so if it's covered somewhere I haven't read about yet, feel free to correct me.

Great work though - I will definitely be using this in my next project.


ActiveRecord for CodeIgniter: Rails-style model interactions - El Forum - 09-21-2007

[eluser]nmweb[/eluser]
Great work, I really like it and am definitely gonna use it in my next project. I, however, need to tweak it to PHP4. At first glance this seems feasible, or is there anything exotic you used?