2011-02-01 25 views
0

我想分页添加到我的代码点火器项目。我为我的模型使用Doctrine,我似乎无法使用$ this-> load-> model('gif')来访问控制器中的方法。我猜一个教义模型的行为是不同的,但肯定有一种方法可以调用公共方法?访问的学说模型(代码点火器)的公共方法

这里是我的控制器:

<?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); 
    } 
} 

这里是我的模型

<?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; 
    } 




} 

我如何可以调用numGifs和getGifs方法从控制器?提前致谢!

+0

它是否会抛出任何错误? – jondavidjohn 2011-02-01 20:49:23

回答

0

我也在使用CI和教义。作为参考,我正在关注位于http://www.phpandstuff.com/articles/codeigniter-doctrine-from-scratch-day-1-install-and-setup的tuto。

,如果你遵循类似步骤,但使用这种方法的模型,我不知道也不需要加载,而是实例化。例如 。

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

(虽然在这种特殊情况下 - $ G预计只有一个 行 - 我不知道,如果我们可以定义表示表本身的模型内部getter函数在政党成员是跟着模型只包含db表的定义以及任何关系)

希望这会有所帮助。