开发者

Accessing public methods in a Doctrine model (Code Igniter)

I'm trying to add pagination to my code igniter project. I am using Doctrine for my models and I can't seem to use $this->load->model('gif') to access the methods in my controller. I guess a Doctrine model acts differently, but surely there is a way to call the public methods?

Here is my controller:

   <?php

class View extends Controller 
{
     function index() 
     {
    // load pagination class
    $gifs = Doctrine::getTable('Gif')->findAll();
    $this->load->library('pagination');
    $config['base_url'] = base_url().'view/';
    $config['total_rows'] = count($gifs);
    $config['per_page'] = '5';
    $config['full_tag_open'] = '<p>';
    $config['full_tag_close'] = '</p>';

    $this->pagination->initialize($config);

    //load the model and get results
    //$this->load->model('gif');
    $data['results'] = $gifs->getGifs($config['per_page'],$this->uri->segment(2));



    // load the view

    $this->load->view('front_images', $data);
  }
}

Here is my model

<?php
class Gif extends Doctrine_Record {

    public function setTableDefinition() 
    {
        $this->hasColumn('photo_path', 'string', 255, array('unique' => true, 'notnull' => true));
        $this->hasColumn('title', 'string', 255, array('notnull' => true));
        $this->hasColumn('user_id', 'integer', 4);
        $this->hasColumn('token', 'string', 255);
    }

    public function setUp() 
    {       
        $this->actAs('Timestampable');      
        $this->hasOne('User', array(
            'local' => 'user_id',
            'foreign' => 'id'
        ));     
    }

    public function preInsert($event) 
    {
        $this->token = (sha1(rand(11111, 99999)));  
    }

    public function numGifs() {

        $result = Doctrine_Query::create()
            ->select('COUNT(*) as num_gifs')
            ->from('Gif')           
            ->fetchOne();
        return $result['num_gifs'];

    }

    public function getGifs($offset, $limit) 
    {

        $gifs = Doctrine_Query::create()            
            ->from('Gif g')         
            ->orderBy('g.created_at DESC')
            ->limit($limit)
            ->offset(开发者_开发技巧$offset)
            ->execute();        
        return $gifs;
    }




}

How can I call the numGifs and getGifs methods from that controller? Thanks in advance!


I am also using CI in conjunction with doctrine. for reference i am following the tuto located at http://www.phpandstuff.com/articles/codeigniter-doctrine-from-scratch-day-1-install-and-setup .

I don't know if you followed similar steps but using this approach models do not need to be loaded but rather instantiated. for eg.

$g = new Gif();
$g = $g->getGifs();

(although in this particular case - $g expects only one row - Am not sure if we can define getter functions inside the model representing the table itself. in the tuto am following the model contains only the db table definition as well as any relationships)

hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜