Neden bu-> modelleri $ ekleyerek.

1 Cevap php

I was playing around with this code. http://programmersvoice.com/tag/code And I noticed that the following line.

$this->load->model($this->models."pagemodel", 'pages');

Ben bu karşılaştırın

$this->load->model("pagemodel", 'pages');

Bu CodeIgniter'ın belge http://codeigniter.com/user_guide/general/models.html#loading önermek budur.

Ancak yöntem 2 ilkinden daha uzun zaman alır.

Herkes "Bu-> modelleri $." Ne açıklayabilir misiniz lütfen do?

Şimdiden teşekkürler.

Aşağıdaki kontrolörleri / admin pages.php bütün kodu

<?php

class Pages extends Application
{

    function Pages()
    {
    	parent::Application();
    	$this->auth->restrict('editor'); // restrict this controller to editor and above
    	$this->load->model($this->models."pagemodel", 'pages'); // Load the page model
    }

    function manage()
    {
    	$data = $this->pages->pages(); // List the pages
    	$this->table->set_heading('Title', 'Slug', 'Actions'); // Setting headings for the table

    	foreach($data as $value => $key)
    	{
    		$actions = anchor("admin/pages/edit/".$key['id']."/", "Edit") . anchor("admin/pages/delete/".$key['id']."/", "Delete"); // Build actions links
    		$this->table->add_row($key['title'], $key['slug'], $actions); // Adding row to table
    	}

    	$this->auth->view('pages/manage'); // Load the view
    }

    function delete($id)
    {
    	$this->pages->delete($id);
    	$this->auth->view('pages/delete_success');
    }

    function add()
    {

    	$this->load->helper(array('form', 'url'));

    	$this->load->library('form_validation');

    	$this->form_validation->set_rules('title', 'Page Title', 'required');
    	$this->form_validation->set_rules('content', 'Content', 'required');

    	if($this->form_validation->run() == FALSE)
    	{
    		$this->auth->view('pages/add');
    	}
    	else
    	{
    		$data['title'] = set_value('title');
    		$data['content'] = set_value('content');
    		$data['slug'] = url_title($data['title'], 'underscore', TRUE);

    		$this->pages->add($data);

    		$this->auth->view('pages/add_success');
    	}
    }

    function edit($id)
    {

    	$this->load->helper(array('form', 'url'));

    	$this->load->library('form_validation');

    	$this->form_validation->set_rules('title', 'Page Title', 'required');
    	$this->form_validation->set_rules('content', 'Content', 'required');

    	if($this->form_validation->run() == FALSE)
    	{
    		$data = $this->pages->page($id);

    		$this->auth->view('pages/edit', $data[0]);
    	}
    	else
    	{
    		$data['title'] = set_value('title');
    		$data['content'] = set_value('content');
    		$data['slug'] = url_title($data['title'], 'underscore', TRUE);

    		$this->pages->edit($id, $data);

    		$this->auth->view('pages/edit_success');
    	}
    }
}

?>

1 Cevap

I'm not totally sure about the following, since the current version of Codeigniter doesn't seem to populate the $this->models değişken, ama sanırım:

$this->models uygulama modelleri dizinin tam yolunu içerdiği, ve bu nedenle yükleme CI farklı klasörlerde bakmak zorunda olmadığından, daha hızlı (küresel ve uygulama)