Source for file generateentity.php
Documentation is available at generateentity.php
require_once 'creole/Creole.php';
/* fix $dsn, $datasource, $outputdir to suit your environment */
$dsn =
array('phptype' =>
'mysql',
'hostspec' =>
'localhost',
'database' =>
'default');
$outputdir =
dirname(__FILE__
).
"/../../../../components/entity";
$conn =
Creole::getConnection($dsn);
$dbinfo =
$conn->getDatabaseInfo();
$base_dir =
$outputdir .
'/'.
'base';
print
"could not make directory for base: {$outputdir}";
foreach($dbinfo->getTables() as $tbl) {
$ar =
split('_', $tablename);
foreach($ar as $key =>
$val) {
$capName =
join('', $ar);
$classname =
"Entity_{$capName}";
$baseclassname =
"Entity_Base_{$capName}";
foreach($tbl->getColumns() as $col) {
$pk =
$tbl->getPrimaryKey();
foreach($pk->getColumns() as $pkcol) {
$fks =
$tbl->getForeignKeys();
$refs =
$fk->getReferences();
//$alias = strtolower($fk->getName());
$alias =
strtolower($refs[0][1]->getTable()->getName());
$refdef[] =
" '{$p}' => '{$c}'";
$buf .=
" '{$alias}' => array(\n";
$buf .=
" 'entity' => '{$entity}',\n";
$buf .=
" 'type' => 'INNER JOIN',\n";
$buf .=
" 'relation' => array(\n";
if (count($joindef_buf)) {
$joindef =
implode(",\n", $joindef_buf);
foreach ($aliases as $alias) {
* @var {
$alias['entity']}
public \${
$alias['name']};
//$cols = array_diff($cols, $pks);
foreach ($cols as $col) {
$coldef .=
" public \$".
$col.
";\n";
//$coldef = "'". implode("',\n '", $cols) ."'";
$pkdef =
"'".
implode("',\n'", $pks) .
"'";
$filepath =
$outputdir .
"/base/{$capName}.php";
print
"{
$tablename}: base class already exists. \n
";
if (!$handle =
fopen($filepath, 'wb')) {
print
"{
$filepath}: could not open file. \n
";
class {
$baseclassname} extends Teeple_ActiveRecord
* 指定が無い場合は、DEFAULT_DATASOURCE で設定されているDataSource名が使用されます。
public static \$_DATASOURCE = '{
$datasource}';
* スキーマを設定する場合は、"スキーマ.テーブル名"とします。
public static \$_TABLENAME = '{
$tablename}';
* プライマリキーとなるカラム名を配列で指定します。
public static \$_PK = array(
* このテーブルのカラム名をpublicプロパティとして設定します。(プライマリキーを除く)
* プライマリキーが自動セット(auto increment)かどうかを設定します。
public static \$_AUTO = TRUE;
$contents =
"<?php \n\n".
$contents .
"\n\n?>";
if (fwrite($handle, $contents) ===
FALSE) {
print
"{
$filepath}: failed to write to the file. \n
";
print
"{
$tablename}: entity base class created . \n
";
$filepath =
$outputdir .
"/{$capName}.php";
print
"{
$tablename}: entity class already exists. \n
";
if (!$handle =
fopen($filepath, 'wb')) {
print
"{
$filepath}: could not open file. \n
";
* Entity Class for {
$tablename}
* エンティティに関するロジック等はここに実装します。
class {
$classname} extends {
$baseclassname}
public static function get() {
return Teeple_Container::getInstance()->getEntity('{
$classname}');
public function find(\$id=null) {
return parent::find(\$id);
* ※generatorが吐き出した雛形を修正してください。
* ここに設定してある定義は、\$this->join('aliasname') で利用できる。<br/>
* 指定方法: 'アクセスするための別名' => 設定値の配列
* 'entity' => エンティティのクラス名
* 'columns' => 取得するカラム文字列(SQLにセットするのと同じ形式)
* 'type' => JOINのタイプ(SQLに書く形式と同じ)(省略した場合はINNER JOIN)
* 'relation' => JOINするためのリレーション設定
* 「本クラスのキー名 => 対象クラスのキー名」となります。
* 'condition' => JOINするための設定だがリテラルで指定するもの
* 'entity' => 'Entity_Fuga',
* 'columns' => 'foo, bar, hoge',
* 'condition' => 'aliasname.status = 1 AND parent.status = 1'
public static \$_JOINCONFIG = array(
$contents =
"<?php \n\n".
$contents .
"\n\n?>";
if (fwrite($handle, $contents) ===
FALSE) {
print
"{
$filepath}: failed to write to the file. \n
";
print
"{
$tablename}: entity class created . \n
";