2009-09-30 3 views
0

다음 모델과 컨트롤러가 있습니다. 데이터베이스 테이블 페이지에는 id, title, content 및 slug가 있습니다.모델에서 두 번째 매개 변수는 무엇입니까 ("pagemodel", 'pages')

Q1. 왜 컨트롤러의 라인은

$this->load->model("pagemodel", 'pages'); // Load the page model, 

에 'pages'가 있습니까?

"pagemodel"을 페이지로 지정하고 있습니까?

내가 선을 참조

$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database 

사용하는 페이지.

모델 코드

<?php 

class Pagemodel extends Model 
{ 

    /** 
    * Constructor 
    */ 
    function Pagemodel() 
    { 
     parent::Model(); 
    } 

    /** 
    * Return an array of pages — used in the manage view 
    * 
    * @access public 
    * @return array 
    */ 
    function pages() 
    { 
     $query = $this->db->query("SELECT * FROM `pages` ORDER BY `id` ASC"); 
     return $query->result_array(); 
    } 

    /** 
    * Return a list of a single page — used when editing a page 
    * 
    * @access public 
    * @param integer 
    * @return array 
    */ 
    function page($id) 
    { 
     $query = $this->db->query("SELECT * FROM `pages` WHERE `id` = '$id' LIMIT 1"); 
     return $query->result_array(); 
    } 

    /** 
    * Return an array of a page — used in the front end 
    * 
    * @access public 
    * @param string 
    * @return array 
    */ 
    function fetch($slug) 
    { 
     $query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'"); 
     return $query->result_array(); 
    } 

    /** 
    * Add a record to the database 
    * 
    * @access public 
    * @param array 
    */ 
    function add($data) 
    { 
     $this->db->query("INSERT INTO `pages` (`title`, `content`, `slug`) VALUES (".$this->db->$data['title'].", ".$this->db->$data['content'].", ".$this->db->escape($data['slug']).")"); 
    } 

    /** 
    * Update a record in the database 
    * 
    * @access public 
    * @param integer 
    * @param array 
    */ 
    function edit($id, $data) 
    { 
     $this->db->query("UPDATE `pages` SET `title` = ".$this->db->escape($data['title']).", `content` = ".$this->db->escape($data['content']).", `slug` = ".$this->db->escape($data['slug'])." WHERE `id` = '$id'"); 
    } 

    /** 
    * Remove a record from the database 
    * 
    * @access public 
    * @param integer 
    */ 
    function delete($id) 
    { 
     $this->db->query("DELETE FROM `pages` WHERE `id` = '$id'"); 
    } 

} 

?> 

컨트롤러 코드

<?php 

class Page extends Application 
{ 

    function Page() 
    { 
     parent::Application(); 
     $this->load->model("pagemodel", 'pages'); // Load the page model 
    } 

    function _view($page, $page_data) 
    { 
     $meta = array (
     'meta_title' => 'title here', 
     'meta_keywords' => 'keywords here', 
     'meta_description' => 'description here', 
     'meta_robots' => 'all', 
     'extra_headers' => ' 
     <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> 

     ' 
     ); 

     $data = array(); 
     // add any data 

     // merge meta and data 
     $data = array_merge($data,$meta); 

     // load view with data 
     $this->load->view('header', $data); 


     $this->load->view($page, $page_data); 
     $this->load->view('footer'); 
    } 

    function index() 
    { 
     $page_slug = $this->uri->segment('2'); // Grab the URI segment 

     if($page_slug === FALSE) 
     { 
      $page_slug = 'home'; //Change home if you change the name in the back-end 
     } 

     $page_data = $this->pages->fetch($page_slug); // Pull the page data from the database 

     if($page_data === FALSE) 
     { 
      show_404(); // Show a 404 if no page exists 
     } 
     else 
     { 
      $this->_view('index', $page_data[0]); 
     } 
    } 




} 

?> 

답변

3
시스템/라이브러리에서

/그래서 그래, 두 번째 매개 변수는 단지 모델의 친근한 이름을 설정하는 것입니다

/** 
* Model Loader 
* 
* This function lets users load and instantiate models. 
* 
* @access public 
* @param string the name of the class 
* @param string name for the model 
* @param bool database connection 
* @return void 
*/ 
function model($model, $name = '', $db_conn = FALSE) 
{ 
    ... 

Loader.php

, 그리고 필요하지 않습니다. 그것은 내 생각에 혼란의 층을 추가합니다.

+0

나는 그것이 혼란의 층을 추가한다고 생각하지 않는다. 그것은 그것을 사용하는 사람에 달려 있습니다. –

0

당신은 아마, '페이지'이름 바꾸기를 시도하고 나는 그것이 컨트롤러에 $ this-> 페이지를 호출하여 더 이상 사용할 수 없습니다 내기 맞아 암호.

관련 문제