まずはソースコードから。
<?php // ライブラリの読み込みを忘れずに App::uses('ComponentCollection', 'Controller'); App::uses('CSVComponent', 'Controller/Component'); class BatchesShell extends AppShell { public $csvComponent = array(); // startupでComponentを使う準備をする public function startup() { $collection = new ComponentCollection(); $this->csvComponent = new CSVComponent($collection); parent::startup(); } /* 省略 */ /** * csv生成 */ private function createCSV($array, $contentsArray) { // こんな感じで使う $this->csvComponent->addRow($array); /* 省略 */ } }
それでは簡単に説明します。
Libraryと使用するComponentの読み込み
ライブラリは最初に読み込まないとツカエナイのでご注意。
<?php // ライブラリの読み込みを忘れずに App::uses('ComponentCollection', 'Controller'); App::uses('CSVComponent', 'Controller/Component');
startup()で、Object生成
ComponentのコンストラクタはComponentCollection
クラスを引数にとる。そのため、こいつも一緒に生成しないといけない。
んで、メンバ変数にObjectを格納してあげれば、同じクラスでどこでも使えるようになります。
<?php // startupでComponentを使う準備をする public function startup() { $collection = new ComponentCollection(); $this->csvComponent = new CSVComponent($collection); parent::startup(); }
こんな感じに用意すれば、Shellの箇所でも使えるようになりますので。覚えておきます( ..)φメモメモ