CodeIgniter Forums
Problem using ActiveRecord Class from Wiki - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem using ActiveRecord Class from Wiki (/showthread.php?tid=15662)



Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]mambe churchill nanje[/eluser]
Hello can someone help me out here with this ActiveRecord
http://codeigniter.com/wiki/ActiveRecord_Class/ class usage,

here is a link to a site am working on, a simple cms I built has this news section

http://www.mpyraa.org/en/cms/newscontroller/

the page works well in localhost but on the server like you must have observed it says
Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'news.*' in 'field list'

SELECT `news`.`*` FROM (`news`) ORDER BY `id` desc LIMIT 6

The code that runs the controller is as follow:

Code:
<?php
/**
* Generic News controller for all afrovisiongroup.com 50k sites that need news implementation
* AfroVisioN News class
*
* @package        CodeIgniter
* @subpackage    Controllers
* @category    Controllers
* @author        Mambe Churchill
* @version        1.0
*/

class newscontroller extends Controller{
    //contstructor
    function newscontroller(){
        parent::Controller();
        $news = & $this->load->model('news');
        $this->load->helper(array('text','typography'));
    }
    //the news index function
    function index(){
        $this->showArticles();
    }
    //show articles function that shows all the articles
    function showArticles($offset=0){
        $this->load->library('pagination');
        $perpage=6;
        $data['offset']=$offset;
        $data['perpage']=$perpage;
        $data['articles']=$this->news->ordering('id','desc')->find_and_limit_by($perpage,$offset);
        $this->load->view('news/home_tpl',$data);
    }
    //create a function that shows a single article
    function showArticle($id,$seo=""){
        $data['article']=$this->news->find($id);
        $this->load->view('news/article_tpl',$data);
    }
}
?>


can someone tell me why this
Code:
$this->news->ordering('id','desc')->find_and_limit_by($perpage,$offset);
works on my localhost and not on the server though both are running php 5 ??


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]TheFuzzy0ne[/eluser]
Sounds more to me like an issue with Database versions than PHP versions.


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]xwero[/eluser]
the load->model method doesn't return an object so you your news variable can't be used to query the database.
I don't know why it works on your localhost but it's a bad construction because CI appends an object to the controller with the name news because the model is named news.

The best solution is to create a method in your news model that gets a part of the table.


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]mambe churchill nanje[/eluser]
well on localhost I use mysql 4 and on the server its mysql 5
but I dont think that could be the problem IMHO, I am not mysql guru Wink

As for the Model not returning and object I dont think it changes anything, but BTW this is a Model that extends activerecords class in the wiki, check my model below

Code:
<?php
    class news extends ActiveRecord{
        function __construct(){
            parent::ActiveRecord();
                $this->_class_name=strtolower(get_class($this));
                $this->_table=$this->_class_name;
                $this->_columns=$this->discover_table_columns();
        }
    }
?>

and in the wiki page I posted earlier for this active record class they actually put $news = &$this->load->model('news') style of code then later you reference $news->delete() or soo
But I wonder why its not working, any ActiveRecord Guru ?? I am checking active record on kohana and I might port the app to Kohana and then use the active record there, but I want it to work in CI because its already done
please any help guys ??


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]xwero[/eluser]
I checked out the Active record class and is seems to be an ORM class. i think you better use a more mature ORM.


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]TheFuzzy0ne[/eluser]
I think the problem is that you are using 'news.*' in your query, and I'm not sure if MySQL 4 supports select calls in this fashion. My suggestion would be to run that SQL statement at the MySQL CLI if you can, and see what it says.


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]mambe churchill nanje[/eluser]
well @xwero can you please point me to a more mature ORM class ??

@TheFuzzyOne, localhost I am using mysql 4 and the query "news.*" worked just fine, on the server its mysql 5 and it fails.
Also the sql gets generated by the ORM class, soo I dont have the SQL written to take and test someone. Could that be the issue


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]TheFuzzy0ne[/eluser]
I'm going to just suggest that you try a new library as xwero suggested. I can't vouch for it as I've never used it, and I can't think of any other possible solutions.


Problem using ActiveRecord Class from Wiki - El Forum - 02-11-2009

[eluser]mambe churchill nanje[/eluser]
tomorrow I will definitely port the codes to kohana and use their ORM instead or maybe take the Kohana ORM and use in codeigniter
thanks guys


Problem using ActiveRecord Class from Wiki - El Forum - 02-12-2009

[eluser]mambe churchill nanje[/eluser]
well I managed to tweak the ORM class from the wiki to do just what I needed it. normally the way I use it now, I deactivated the use of HAS_MANY and all the other relationship related ORM.
I adjusted the code from this
Code:
/**
     * _compile
     *
     * This function compiles a list of fields to select.
     *
     * @author    Anton Muraviev
     * @access    private
     * @return    void
     */
    function _compile ()
    {
        $this->_select[] = $this->_table . '.*';
        $this->db->select(implode(',',$this->_select));
    }
to this
Code:
/**
     * _compile
     *
     * This function compiles a list of fields to select.
     *
     * @author    Anton Muraviev
     * @access    private
     * @return    void
     */
    function _compile ()
    {
        //$this->_select[] = $this->_table . '.*';
        $this->_select[] = '*';
        $this->db->select(implode(',',$this->_select));
    }
but anything query that has to do with relationship like fetch_related will not compile the right way because it will involve two tables, but it works for me and how I need it now. when next I upgrade I will use the ORM in Kohana. thanks y'all for your help