2011-02-01 13 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表的定義以及任何關係)

希望這會有所幫助。