• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ActiveRecord for CodeIgniter: Rails-style model interactions


I'm really interested in the idea of a slim auth library based on this. I was about to try out erkana auth because i like the philosophy behind it. The first thing I thought though is how can I alter this thing to work with this active record mod.
Let us know how you get on.

Will keep you posted. I'm thinking I'll prefix any libraries with 'Ar_' to signify their requirement of this library.

[eluser]Matthew Pennell[/eluser]
[quote author="easylancer" date="1202572009"]I need a PHP 4 version of the ActiveRecord Library, anyone has it?[/quote]
I think someone had a bash at one in the first couple of pages of this thread, but AFAICR it's not possible - PHP4 doesn't do overloading and doesn't have a direct equivalent of the __call() method to pick up unknown method calls, so none of the wildcard methods will be possible.

Upgrade to PHP5! Wink


I just wrote a whole load of crap because i didn't take the time to read your code properly.
Keep up the good work Wink

if you're still having problems getting a plain object returned PM me.

Ok now I've got a real question.
I'm having trouble with some join relationships

I have the table users which has the following fields

and profiles which has

What I'd like to do is retrieve a user by id or by email while joining profiles
I've tried the following code
$user = $this->user->join($this->profile)->find_by_id(2,RETURN_NEW,ACTIVE_RECORD);

but I get the an sql error:

Unknown column 'users.profile_id' in 'on clause'

SELECT users.*, user_profiles.user_id AS profile_user_id, user_profiles.first_name AS profile_first_name FROM users LEFT JOIN profiles AS user_profiles ON users.profile_id = user_profiles.id WHERE users.id = 2

The correct sql I'd like it to produce is

SELECT users.*, user_profiles.user_id AS profile_user_id, user_profiles.first_name AS profile_first_name FROM users LEFT JOIN profiles AS user_profiles ON users.id = user_profiles.user_id WHERE users.id = 2

I can find by profile joining users and it works fine because profile has the user_id but that's not my requirement

I've tried changing the relationship statements in the profile and user models to every combination of _has_one and _belongs_to but it doesn't seem to make a difference.

Am I missing something. Can you only join through a child object?


really nice

[quote author="frith" date="1203433722"]Am I missing something. Can you only join through a child object?[/quote]

I had the same difficulties, but I think through the child is required. In the end, writing $this->profile->join($this->user) and then finding by 'user_id' is still fairly clear. Users and profiles are usually separated by choice rather than necessity, since you'd normally only ever have one unique unit between the two tables. You might even dump the id off of profiles and go solely with the foreign key 'user_id' as index. Unless you are allowing more than one profile per user.

Hi gurus, I need some help here with this, I am getting some weird behaviors and not sure why. Please let me know if I am wrong, I felt like this is possessed by some gremlins Confusedhut:

Here is what happening:

I am getting this error message bellow, somehow the $num_[...] variable seems to not been set

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$num_pages

Filename: controllers/orch.php

Text model is like this:
class Text extends ActiveRecord {

    function __construct ()
        $this->_belongs_to = array('pages');

Page model is like this:
class Page extends ActiveRecord {

    function __construct ()
        $this->_has_many = array('texts');

This is my controller
class ORCH extends Controller
    function ORCH()
    function index()
        $pages = $this->page->count_pages()->find_all();        
        foreach($pages as $page)
            echo '<h3>'.$page->pg_title.'</h3>';
            echo '<p>'.$page->pg_description.'</p>';
            echo '<h1>'.$page->num_pages.'</h1>';    

I am using the development version which uses the count_ method instead of counting_, but even if I try with the stable version I get the exact same error.

Can some one help me please or else the gremlins will take me :grrr:

are you sure you don't you want $pages = $this->page->count_texts()->find_all(); ?
I'm not really sure what your requirements are but if you're looking for number of pages I would suggest counting the rows returned from $this->page->find_all().
Using count_pages in the way you have doesn't make a lot of sense as you are already searching pages so it might try to join pages to pages. That construct should be used to count related records which is why I suggest that perhaps you want count_texts. Also unless $page->num_pages is actually a field in your pages table there isn't going to be any data there as I believe count would name the field like texts_count or pages_count. Try doing a var dump on $page to have a look what's in there. $this->db->last_query() is a useful tool as well to make sure active record is generating the sql you expect.
Perhaps you could tell us what it is you'd like to achieve in the query.


Hi everybody. Really been enjoying using this class. I'm sure I'll have questions from time to time but this time I thought I'd share a slight modification I made.

I needed to find a way to count records that matched a certain criteria. SELECT COUNT...WHERE... sort of thing.
$this->users->where('email',$email_str)->count(); would have worked perfectly well but I have this paranoid feeling that as soon as you start chaining where's to your query your controller begins to look a bit like a model.

I wanted something a bit more controller friendly so I created a dynamic function similar to the find_by_fieldname function, called find_count_by_fieldname so now my call ends up looking like $this->users->find_count_by_email($email_str); Not a whole lot different I know, but somehow it just sits a bit better with me having something like that in the controller.

Anyway here are the changes I made. I was using a ChromeIce's development version.

function __call ($method, $args)
        $watch = array(

and just below the _find_by function I added

     * _find_count_by
     * Count records in table where condition is met.
     * There are some special search terms that you can use for particular searches:
     * IS_NULL to find null or empty fields NOT_NULL to find fields that aren't
     * empty or null
     * @access    private
     * @param    string
     * @param    array
     * @return    object
    function _find_count_by ($field, $args)
        $field = $this->_table . '.' . $field;

        switch ( $args[0] )
            case IS_NULL:
                $this->where($field . IS_NULL);
            case NOT_NULL:
                $this->where($field . NOT_NULL);
                $this->where($field, $args[0]);

        return $this->count(null,( isset($args[1]) ) ? $args[1] : null, ( isset($args[2]) ) ? $args[2] : null );


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.