CakePHPã§RDB以å¤ã®ãã¼ã¿ã½ã¼ã¹ã使ãæã®Bakeã®ã¨ã©ã¼ã¨åé¿
CSVããã¼ã¿ã½ã¼ã¹ã¨ãã¦æ±ããããã«ãã CsvSource ã¨ãããã©ã°ã¤ã³ãããã¾ãã
ã³ã³ããã¼ã©ã¼ã® $uses ã«CSVã使ãã¢ãã«ãè¨å®ããããBake 㧠View ãç¼ãéã«ã¨ã©ã¼ãåºãããã«ãªã£ã¦ãã¾ãã¾ããã
CsvSource ã¨ã¯
http://github.com/cakephp/datasources
CakePHP ã® github ã« datasources ã¨ãããªãã¸ããªãããã¾ãã
CsvSource ãã¯ãããããã¤ãã®ãã¼ã¿ã½ã¼ã¹ãæ±ãããã©ã°ã¤ã³ã«ãªã£ã¦ã¾ãã
ã¨ã©ã¼ã®ç¶æ³
<?php class Csv extends AppModel{ public $useDbConfig = 'csv'; }
CsvSource ã使ãã¨ãRDBã§ã¯ãªãCSVããã¼ã¿ã½ã¼ã¹ã¨ãã¦æ±ããã¢ãã«ãä½ãã¾ãã
<?php class ExamplesController extends AppController { public $name = 'Examples'; public $uses = array('Example', 'Csv'); public function index(){ $this->Csv->find(); }
public $uses = array('Example', 'Csv'); ã¨ããäºã§ããã®ã³ã³ããã¼ã©ã¼ã§Csvã¢ãã«ã使ããããã«ãªãã¾ãã
--------------------------------------------------------------- Interactive Bake Shell --------------------------------------------------------------- [D]atabase Configuration [M]odel [V]iew [C]ontroller [P]roject [F]ixture [T]est case [Q]uit What would you like to Bake? (D/M/V/C/P/F/T/Q) > v --------------------------------------------------------------- Bake View Path: /Users/myuser/Dev/cakephp1.3.0/../cakephp_baked/views/ --------------------------------------------------------------- Possible Controllers based on your current database: 1. Examples Enter a number from the list above, type in the name of another controller, or 'q' to exit [q] > 1 Would you like bake to build your views interactively? Warning: Choosing no will overwrite Hospitals views if it exist. (y/n) [n] > Error: Missing database table 'csvs' for model 'Csv'
ãããªæãã§ãBake 㧠View ãç¼ãæã«ã¨ã©ã¼ãåºãããã«ãªã£ã¦ãã¾ãã¾ããã
Controller ã Model 㯠Bake ã®å¿
è¦ããªãã£ãã®ã§ãã£ã¦ã¾ããããåæ§ã®ã¨ã©ã¼ãèµ·ããããããã¾ããã
解決ç
<?php class ExamplesController extends AppController { public $name = 'Examples'; public function index(){ $this->loadModel('Csv'); $this->Csv->find(); }
public $uses ã¯ããã©ã«ãã®ã¾ã¾ã«ãã¦ããã¦ãloadModel() ã¡ã½ããã§ä½¿ãæã«ãã¼ãããå½¢ã«ãã¾ãã
ãã®æ¸ãæ¹ãªã View ã Bake ã§ç¼ãæã«ããã¨ã©ã¼ããªãå¦çã§ãã¾ããã
è£è¶³
ã³ã³ããã¼ã©ã®ä¸ã§ã$usesã§ã¢ãã«ãããã¤ãæ¸ãã¦ããã°ã
$this->Model1->find();
$this->Model1->Model2->find();
ã¿ããã«ãã¦å¿«é©ã«ã¢ãã«ã«ã¢ã¯ã»ã¹ã§ããã®ã§ããããã®$usesã®é åã«ã¢ãã«åãå ãã¦ããã¨ãä¸ã¤ã«ã¤ãã4-6%表示æéãå¤ããããã¨ã®ãã¨ã試ãã«7ã¢ãã«ã$usesã§èªã¿è¾¼ãããã«ãããã40%ã表示æéãå¢ããããã§ã (追è¨ï¼ãã¶ããã®æ¯è¼ã¯ãããã¢ã¯ã·ã§ã³ã§ä½¿ãã¢ãã«ã1ã¤ã ã£ãå ´åã«ã$usesã§7ã¢ãã«å®ç¾©ããå ´åã¨ã$usesã«ã¯ä½ãå®ç¾©ããã該å½ã¢ã¯ã·ã§ã³å ã§1ã¤ã®ã¢ãã«ã ãloadModelããå ´åã§æ¯è¼ãã¦ãã£ã½ã)
Modelを2個以上読み込む場合は、$usesを使わない方がスピードアップする - cakephperの日記(CakePHP, Laravel, PHP)
ããã©ã¼ãã³ã¹ã®è¦³ç¹ããã loadModel() ã§ä½¿ãæã ãã«è¨å®ããæ¹ãè¯ãã¿ããã§ãã
<?php class DATABASE_CONFIG { var $default = array(...); var $csv = array( 'datasource' => 'datasources.csvSource', 'path' => '../tmp/csv', // local path on the server(windows) 'extension' => 'csv', // file extension of CSV Files 'readonly' => true, // only for reading 'recursive' => false, // only false is supported at the moment ); }
ã¡ãªã¿ã«ãCsvSource ã使ãããã® app/config/database.php ã¯ãããªæãã