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

[eluser]Maurice Calhoun[/eluser]
My bad, and remove the chaining... I think that is it. I could not post the entire class.

Code:
/**
     * discover_table_columns
     *
     * Called on instantiation of a model to capture the field names
     * of the related table. By convention, the model name is singular
     * and the table name is plural.
     *
     * @access    public
     * @return    object
     */    
    function discover_table_columns()
    {
        $show = $this->db->query('SHOW COLUMNS FROM ' . $this->_table);
        return $show->result();
    }

Code:
/**
     * find
     *
     * Basic find function. Either pass in a numeric id to find that table row,
     * or an array of key/values for a more complex search. Note that passing in
     * an array of 1 is stupid, as you can use find_by_fieldname() instead.
     *
     * To simply return all records, use the ALL constant: $myobj->find(ALL);
     *
     * @access    public
     * @param    int || array
     * @return    object || array
     */    
    function find($args)
    {
        if (is_array($args))
        {
            foreach ($args as $key => $value)
            {
                if (!isset($first_key))
                {
                    $first_key = $key;
                    $first_value = $value;
                }
            }
            array_shift($args);
            $data = array(
                $first_value,
                $args
            );
            return $this->find_all_by($first_key, $data);
        }
        else
        {
            if ($args != ALL)
            {
                $this->db->where('id', $args);
                $this->db->from($this->_table);
                $query = $this->db->get();
                if ($query->num_rows() > 0)
                {
                    eval('$return = new ' . $this->_class_name . '();');
                    $found = $query->row();
                    foreach($this->_columns as $column)
                    {
                        eval('$return->' . $column->Field . ' = $found->' . $column->Field . ';');
                    }
                    return $return;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                $this->db->from($this->_table);
                $query = $this->db->get();
                foreach ($query->result() as $row)
                {
                    eval('$x = new ' . $this->_class_name . '();');
                    foreach ($this->_columns as $column)
                    {
                        eval('$x->' . $column->Field . ' = $row->' . $column->Field . ';');
                    }
                    $return[] = $x;
                    $x = null;
                }
                
                return $return;
            }
        }
    }


Messages In This Thread
ActiveRecord for CodeIgniter: Rails-style model interactions - by El Forum - 09-24-2007, 02:33 PM



Theme © iAndrew 2016 - Forum software by © MyBB