【问题标题】:What is the second parameter doing in model("pagemodel", 'pages')model("pagemodel", 'pages') 中的第二个参数在做什么
【发布时间】:2009-09-30 06:43:24
【问题描述】:

我有以下型号和控制器。而数据库表页有id、title、content和slug。

第一季度。为什么控制器里的线,

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

有“页面”吗?

是否将“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]);
        }
    }




}

?>

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    来自系统/库/Loader.php

    /**
    * 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)
    {
        ...
    

    所以是的,第二个参数只是为模型设置一个“友好”的名称,不需要。在我看来,这增加了一层混乱。

    【讨论】:

    • 我不认为它增加了一层混乱。这取决于使用它的人。
    【解决方案2】:

    您可能是对的,尝试重命名“页面”,我敢打赌,通过调用它不再可用 $this->控制器代码中的页面。

    【讨论】:

      猜你喜欢
      • 2012-05-04
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      相关资源
      最近更新 更多