Welcome Guest, Not a member yet? Register   Sign In
ActiveRecord for CodeIgniter: Rails-style model interactions
#40

[eluser]chrisj[/eluser]
Thanks BB & NM.

I found some major bugs with my _fetch_related function, however. Here's the new and improved version:
Code:
function _fetch_related($table, $inflected)
    {
        $inflected = ($inflected) ? $inflected[0] : substr($table, 0, -1);

        if( array_search($table, $this->_belongs_to) !== false ) {
            $foreign_key = $table . '_id';
            $query = $this->db->query('SELECT * FROM ' . $inflected . ' WHERE id = ' . $this->$foreign_key);
        }
        else if( array_search($table, $this->_has_many) !== false || array_search($table, $this->_has_one) !== false ) {    
            $query = $this->db->query('SELECT * FROM ' . $table . ' WHERE '. $this->_class_name .'_id = id');
        }                    
        else if( array_search($table, $this->_has_and_belongs_to_many) !== false ) {
            $relationship_table = ($this->_table < $table) ? $this->_table . '_' . $table : $table . '_' . $this->_table;
            $query = $this->db->query('
                SELECT
                    ' . $table . '.*
                FROM
                    ' . $table . '
                LEFT JOIN
                    ' . $relationship_table . '
                ON
                    ' . $table . '.id = ' . $inflected . '_id
                LEFT JOIN
                    ' . $this->_table . '
                ON
                    ' . $this->_table . '.id = ' . $this->_class_name . '_id
                WHERE
                    ' . $this->_table . '.id = ' . $this->id
            );
        }
        else {
            return false;
        }

        eval('$this->' . $table . ' = $query->result();');            
    }


Messages In This Thread
ActiveRecord for CodeIgniter: Rails-style model interactions - by El Forum - 10-02-2007, 08:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB