Welcome Guest, Not a member yet? Register   Sign In
Model extending CI_DB_active_record (Please Help Rick, Derek)
#1

[eluser]Maurice Calhoun[/eluser]
Active Record has a method $this->db->get();
Quote:Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table

But this does not make any sense to do this

Here is my Conroller
Code:
class Blog extends Controller{

    function Blog(){

        parent::Controller();
        $this->load->model('Post','post');

    }

    function index(){
      $all_posts = $this->post->get('posts'); // This do not make sense
      $last_ten_posts = $this->post->get_last_ten(); // This make sense
    }

}

Here is my Model
Code:
class Post extends Model{

    function Post(){

         parent::Model();
         $this->load->database();

    }

    // This do not make sense
    function get($table_name){
         return $this->db->get($table_name);
    }

    // This make sense
    function get_last_ten(){
         return $this->db->get('POSTS',10,10);
    }

}

This seem redundant to me

Is there a way to do just this my Controller
Code:
class Blog extends Controller{

    function Blog(){

        parent::Controller();
        $this->load->model('Post','post');

    }

    function index(){
      $posts = $this->post->get('posts');
    }

}

Without have to recreate the same method that Active Record already has in the Model
#2

[eluser]rogierb[/eluser]
You migth try something in your model like:
Code:
function getSomeTable($fields=null, $limit=null, $where=null)
    {    
        ($fields != null) ? $this->db->select($fields) :'';
        
        ($where != null) ? $this->db->where($where) :'';
        
        ($limit != null ? $this->db->limit($limit['start'], $limit['end']) : '');
        
        //returns the query string
        return $this->db->get($this->_table);
    }

where $this->_table is defined in the contructor.

Ow, I 'borrowed' the above code somewhere, so credit goes to... well, not me.
#3

[eluser]Maurice Calhoun[/eluser]
Thanks, but thats not it?

Sorry,
What I want to do, is not to recreate Active Record methods. But extend the Model to them, so in my Controller I can do
Code:
$this->'model_name'->'active_record-method'()
is instead of
Code:
$this->db->'active_record_method'()
or recreating and Active Record methods in my Model like
Code:
function get(){
    return $this->db->get('table_name');
}
#4

[eluser]gungbao[/eluser]
most clean way to achieve it is to construct your derived objects against interfaces !!!



or

check the loader class, there is a nice code-statement:

self:'bla' = $this, means you could try to link an alias pointer into the db-class functions

self:get = &db;::activerecordmethod


just an theoretical idea, try it yourself :-) and pls. let us know...
#5

[eluser]Maurice Calhoun[/eluser]
gungbao,

Thanks for the reply, but with the example I have above, how can I do what you have stated.
What would be the syntax, and where would that syntax be placed?
#6

[eluser]Derek Allard[/eluser]
You don't have to use a model. You could just call get directly in the controller, but if you want to use models, and you simply want to retrieve the whole table, exactly as db->get() does, then the way you've done it looks right to me.

I'm moving this point into CI discussion where I think its a bit better suited.
#7

[eluser]Maurice Calhoun[/eluser]
Derek,

Yes, I know I can call db->get() from the controller, but I would like to keep the same name convention...

If I have a Model name Post, and inside a method name "show_last_ten" the syntax would look like this ...

Code:
$this->post->show_last_ten();

Now the ActiveRecord has a method 'get', but in the controller the syntax is...

Code:
$this->db->get();

I would like to do (the same as ActiveRecord)....

Code:
$this->post->get();

but without having to re-create the 'get' method in my Model

Do that make any sense? I just want to keep the same naming convention, this just looks funny to me in the controller...

Code:
$this->db->get();
$this->post->show_last_ten();




Theme © iAndrew 2016 - Forum software by © MyBB