2009-08-20 4 views

답변

0

나는이 방법 중 하나를 사용하지 않을 것입니다. YAML도 사용하지 않을 것입니다. 나는 당신의 "긴 PHP 배열"을 만들 것입니다. 긴 PHP 배열을 기다리십시오.

YAML로 저장하기 위해 PHP 배열로 데이터를 저장하는 것과 별반 다르지 않습니다. 또한 배열로 저장하면 루프를 통해 YAML에서 배열로 변환 할 필요가 없습니다.

이 배열의 적절한 위치는 Configure 클래스로로드하는 파일입니다. 예 : /app/config/form.php :

<?php 
$config = array(
    'Form' => array(
    ... 
) 
); 
?> 

그런 다음이

<?php 
Configure::load('form'); // Loads app/config/form.php file and stores $config in Configure class 
$form = Configure::read('Form'); 
foreach ($form as $field) { 
    ... 
} 
?> 

난 그냥 배열을 사용 http://book.cakephp.org/view/415/load

+0

참조 앱에서 원하는 위치에서 배열 및 액세스를로드 할 수 있습니다. .. – brndnmg

3

귀하는이 데이터를 YAML 형식으로 저장하고자 함을 명시하셨습니다. MVC 용어에서 이것은 이제 당신의 데이터 소스이다.

시스템 어디서나 YAML 파일을 저장할 수는 있지만 버전 제어하에 저장하고 공개적으로 액세스하기를 원하지 않는다고 가정하면 적절한 장소는 외부의 app/ 디렉토리에 있습니다. app/webroot/입니다.

내가 아는 한, 현재 YAML 파일을 읽거나 쓰도록 고안된 기존 데이터 소스가 없으므로 MVC 구조를 엄격히 준수하고자한다면이 작업이 바람직 할 것입니다.

참조 할 수있는 데이터 소스가 여러 가지 있습니다 (on githubother places). 데이터 소스를 생성하는 데는 두 가지 공통적 인 접근 방식이 있다는 점에 주목할 가치가 있습니다.

  • 간단한 접근 방식 (주로 가장 일반적)은 데이터 읽기/쓰기와 관련이 있으며 모델에서 호출 할 수있는 데이터 소스에서 사용자 지정 메서드를 사용하는 경향이 있습니다. 이것은 가장 빠른 접근 방법이며, 모든 CakePHP의 규칙을 따라 잡아야 할 필요가없는 데이터를 가져올 수 있습니다.
  • 더 진보 된 접근 방식 (케이크 코어 개발자가 선호하는)은 확장하려는 클래스의 대부분의 메소드를 올바르게 다시 구현하는 것입니다 (DataSource 또는 데이터베이스의 경우 DboSource). 이것은 find('list')이나 paginate()과 같은 일반적인 작업을 수행 할 때 CakePHP가 마술을 부리는 것을 허용합니다.

개인적으로 너무 같은 간단한 방법으로 시작 권합니다 :

app/config/database.php :

var $yaml = array(
    'datasource' => 'yaml', 
    'path' => 'data/yaml', // relative to app/ directory, no trailing slash 
); 

app/models/category.php :

// app/models/category.php 
class Category extends AppModel { 

    public $useDbConfig = 'yaml'; 

    /** 
    * cake should automatically set $model->table to 'categories' on construct 
    * (http://api.cakephp.org/view_source/model/#l-419) hence $useTable will not 
    * be necessary (convention over configuration) 
    */ 
    // public $useTable = 'categories'; 

    /** 
    * since datasource doesn't strictly adhere to cake's api, the core find 
    * method may cause errors. to combat this we can redefine this method with 
    * a much simpler version, but functionality will be lost in doing so. 
    */ 
    public function find($type = 'all', $options = array()) { 
     // get an instance of our datasource 
     $ds = ConnectionManager::getDataSource($this->useDbConfig); 
     // $type is usually handled by model, but we will pass it to datasource 
     $options['type'] = $type; 
     // query the datasource 
     return $ds->read($this, $options); 
    } 

} 

app/models/datasources/yaml_source.php :

:

class PostsController extends AppController { 

    /** 
    * this is only needed if all actions use these models. Since Post model is 
    * loaded automatically, we can just use Controller::loadModel() to get an 
    * instance of Category model where needed. 
    */ 
    //public $uses = array('Post', 'Category'); 

    public function edit($id) { 
     $posts = $this->Post->read(null, $id); 
     if (!$post) { 
      $this->cakeError('error404'); 
     } 
     $this->loadModel('Category'); 
     $categories = $this->Category->find('list'); 
     $this->set(compact('posts', 'categories')); 
    } 

} 

당신은 더 많은 기능 (매김, 콜백 필터, 관계 등)을 필요로합니다 (API에 더 호환 만들기) 시간을 통해이 데이터 소스에 확장 할 수있을 것입니다. 이 데이터 소스는 다른 응용 프로그램 (s ') 모델에서 즉시 재사용 할 수 있습니다.

0

더 간단한 방법 (읽기 : 더 기본) 여전히 질문에 대한 대답하지만, 다음과 같이 될 것이다하는 YAML 파일에 정적 데이터를 저장할 사실을 무시하는 :

// app/models/category.php 
class Category extends AppModel { 

    public $name = 'yaml'; 

    public $data = array(
     array(
      'id' => '1', 
      'name' => 'Category 1', 
     ), 
     array(
      'id' => '2', 
      'name' => 'Category 2', 
     ), 
     ... 
    ); 

    public function find($type = 'all', $options = array()) { 
     if ($type == 'all') { 
      return $this->data; 
     } 
     // .. otherwise do some array manipulation on $this->data .. 
     return $results; 
    } 
} 

이 방법 아무튼를 데이터 소스를 사용하는 것과 동일한 확장 성 또는 재사용 가능성을가집니다.

관련 문제