2009-08-20 17 views

答えて

0

私はそれは、これらの方法のいずれかをしないだろう。 YAMLも使用しません。私は長いPHP配列を待って、あなたの "長いPHP配列"を作成します。

YAMLとしてそれを格納するPHP配列としてデータを格納することに大きな違いはありません。さらに、配列として格納する場合は、配列をループするためにYAMLから配列に変換する必要はありません。

この配列のための適切な場所では、Configureクラスで読み込むファイルです。例えば。 /app/config/form.php:

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

あなたは、それは私がちょうど配列を使用http://book.cakephp.org/view/415/load

+0

を参照してください

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

あなたのアプリとのアクセスに好きな場所からの配列を読み込むことができます。 .. – brndnmg

3

このデータをYAML形式で保存することを希望しました。 MVCの用語では、これがあなたのデータソースになりました。

システム上の任意の場所にYAMLファイルを保存できますが、これらをバージョン管理下に保存し、一般にアクセスできないようにしたい場合は、app/ディレクトリのどこにでも適切な場所がありますapp/webroot/である。

私が知る限り、現在YAMLファイルを読み書きするための既存のデータソースはありません。したがって、MVC構造を厳密に遵守したい場合は、これが自分の仕事です。

参照可能なデータソースの例は、on githubother placesです。データソースを作成するための2つの一般的なアプローチがあることは注目に値する。

  • 単純なアプローチ(および最も一般的)は、主にデータの読み書きに関係し、モデルが呼び出せるデータソースでカスタムメソッドを使用する傾向があります。これは最も速いアプローチであり、すべてのCakePHPの慣習では必ずしも必要ではないデータを引き出すことができます。
  • さらに進んだアプローチ(ケーキコア開発者が好むかもしれません)は、拡張するクラスのほとんどのメソッドを適切に再実装することです(DataSourceまたはデータベースの場合はDboSource)。これにより、CakePHPは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 YamlSource extends DataSource { 

    public function read(&$model, $queryData = array()) { 
     // determine path to yaml file 
     $yamlPath = APP . $this->config['path'] . DS . $model->table . '.yaml'; 
     // parse the yaml file 
     $results = $this->_parseYaml($yamlPath); 
     // perform any array manipulation (determined by $queryData) 
     ... 
     // handle $queryData['type'] (would normally have been done in Model) 
     ... 
     // return results in a typical Model data array 
     return array($model->alias => array(
      $results // your results go here 
     )); 
    } 

} 

app/controllers/categories_controller.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; 
    } 
} 

は、このアプローチのdoesnデータソースを使用するのと同じ拡張性または再利用性を持っていません。

関連する問題