Part of the EllisLab Network
   
1 of 12
1
ActiveRecord for CodeIgniter: Rails-style model interactions
Posted: 20 September 2007 06:14 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  326
Joined  05-07-2006

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:

$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.

 Signature 

The Watchmaker Project | My EE add-ons | Follow me on Twitter

Profile
 
 
Posted: 20 September 2007 08:32 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  53
Joined  05-30-2007

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

 Signature 

Alex Sancho
Personal Weblog
Web Studio

Profile
 
 
Posted: 20 September 2007 08:40 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  326
Joined  05-07-2006
alexsancho - 20 September 2007 08:32 AM

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?

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.

 Signature 

The Watchmaker Project | My EE add-ons | Follow me on Twitter

Profile
 
 
Posted: 20 September 2007 11:22 AM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2264
Joined  07-30-2007

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).

 Signature 

Become a fan of the CodeIgniter Cookbook (estimated: Fall 2010).

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 20 September 2007 12:53 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  326
Joined  05-07-2006
Buddy Bradley - 20 September 2007 08:40 AM

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.

I’ve added the basic update() method to the downloadable code and the wiki article now. smile

 Signature 

The Watchmaker Project | My EE add-ons | Follow me on Twitter

Profile
 
 
Posted: 21 September 2007 04:54 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  366
Joined  04-27-2006

Hi!

and how can I use the update() ??

like this???

$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!!

Profile
 
 
Posted: 21 September 2007 05:32 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  326
Joined  05-07-2006
gunter - 21 September 2007 04:54 AM

and how can I use the update() ??

Like this:

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

The Watchmaker Project | My EE add-ons | Follow me on Twitter

Profile
 
 
Posted: 21 September 2007 11:35 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  515
Joined  12-05-2006

Thanks for this nice library Buddy!

I’ll check it out!

Dan

 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 21 September 2007 12:17 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2264
Joined  07-30-2007

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.

 Signature 

Become a fan of the CodeIgniter Cookbook (estimated: Fall 2010).

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 21 September 2007 02:45 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
RankRankRank
Total Posts:  303
Joined  10-17-2006

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?

Profile
 
 
Posted: 22 September 2007 01:50 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  326
Joined  05-07-2006
walesmd - 21 September 2007 12:17 PM

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.

You’re right (and there isn’t any other kind of relationship defined). The difficulty is that there isn’t a built-in :has_many or similar pre-defined relationship archetypes like RoR has.

Perhaps if you specify an array of relationships in the constructor of the model, something like this:

<?php

class Page extends ActiveRecord {

    
function __construct()
    
{
        parent
::ActiveRecord();
        
$this->_class_name = strtolower(get_class($this));
        
$this->_table = $this->_class_name . 's';
        
$this->_has_one = array('author');
        
$this->_belongs_to = array('site');
        
$this->_has_and_belongs_to_many = array('resource');
        
$this->_columns = $this->discover_table_columns();
    
}
    
}

?>

And then the create_relationship() and fetch_related() functions would refer to those relationship properties when building their queries.

Does that make sense?

 Signature 

The Watchmaker Project | My EE add-ons | Follow me on Twitter

Profile
 
 
   
1 of 12
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115012 Total Logged-in Users: 68
Total Topics: 122446 Total Anonymous Users: 5
Total Replies: 647325 Total Guests: 535
Total Posts: 769771    
Members ( View Memberlist )