產生

程式码產生可用於建立許多重複的程式码为你加速开发時間, 这完全是選擇性的 - 就像所有的 Oil - 且事後你可以编輯所有程式码,你可以產生下列項目:

控制器

產生一个預定義有行动與檢視的骨架 控制器, 使用以下命令:

$ php oil g controller posts action1 action2 action3
	Created view: APPPATH/views/posts/action1.php
	Created view: APPPATH/views/posts/action2.php
	Created view: APPPATH/views/posts/action3.php
	Created controller: APPPATH/classes/controller/posts.php

这将產生一个控制器看起來像这樣:

class Controller_Posts extends Controller_Template
{

	public function action_action1()
	{
		$this->template->title = 'Posts » Action1';
		$this->template->content = View::forge('posts/action1');
	}

	public function action_action2()
	{
		$this->template->title = 'Posts » Action2';
		$this->template->content = View::forge('posts/action2');
	}

	public function action_action3()
	{
		$this->template->title = 'Posts » Action3';
		$this->template->content = View::forge('posts/action3');
	}

}

/* End of file posts.php */

模型

藉由表列欄位產生一个简单的模型且有自动为你建立匹配的 迁移

$ php oil g model post title:varchar[50] body:text user_id:int
	Created model: APPPATH/classes/model/post.php
	Created migration: APPPATH/migrations/001_create_posts.php

那将建立一个使用 Orm 的简单模型,所以確認在你的配置档案中套件已经啟用,它看起來像这樣:

class Model_Post extends Orm\Model {

	protected static $_properties = array(
		'id',
		'title',
		'body',
		'created_at',
		'updated_at'
	);

	protected static $_observers = array(
		'Orm\Observer_CreatedAt' => array(
			'events' => array('before_insert'),
			'mysql_timestamp' => false,
		),
		'Orm\Observer_UpdatedAt' => array(
			'events' => array('before_save'),
			'mysql_timestamp' => false,
		),
	);

}

/* End of file post.php */

不是非常令人興奮,但迁移在这裡是有用的部分:

namespace Fuel\Migrations;

class Create_posts
{
	public function up()
	{
		\DBUtil::create_table('posts', array(
			'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
			'title' => array('constraint' => 50, 'type' => 'varchar'),
			'body' => array('type' => 'text'),
			'user_id' => array('constraint' => 11, 'type' => 'int'),
			'created_at' => array('type' => 'datetime'),

		), array('id'));
	}

	public function down()
	{
		\DBUtil::drop_table('posts');
	}
}

如果你不希望產生迁移,只需提供 --no-migration

$ php oil g model post title:varchar[50] body:text user_id:int --no-migration
	Created model: APPPATH/classes/model/post.php

預設情況下,產生的类別名稱是单数(它代表一篇发文),但產生的相應資料表是複数(包含多篇发文)。 你可以让資料表使用與模型相同的名稱透過使用 --singular

使用 Model_Crud 產生模型

FuelPHP v1.1 添加一个简单的 Model_Crud 基础模型,提供类似的功能,使用沒有關聯資料開銷的 ORM ,你可以透過添加 --crud 產生模型。

$ php oil g model post title:varchar[50] body:text user_id:int created_at:datetime --crud
	Created model: APPPATH/classes/model/post.php
	Created migration: APPPATH/migrations/001_create_posts.php

那将建立一个使用 Fuel\Core\Model_Crud 的简单模型。它看起來像这樣:

class Model_Post extends \Model_Crud
{
	protected static $_properties = array(
		'id',
		'title',
		'body',
		'user_id',
		'created_at',
		'updated_at'
	);

	protected static $_table_name = 'posts';

}

產生沒有時間戳記選項的模型

添加 --no-timestamp 以排除 建立/更新 的欄位與觀察者。

$ php oil g model post title:varchar[50] body:text user_id:int --no-timestamp
class Model_Post extends \Orm\Model
{
  protected static $_properties = array(
    'id',
    'title',
    'body',
    'user_id'
  );

}
namespace Fuel\Migrations;

class Create_posts
{
  public function up()
  {
    \DBUtil::create_table('posts', array(
      'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
      'title' => array('constraint' => 50, 'type' => 'varchar'),
      'body' => array('type' => 'text'),
      'user_id' => array('constraint' => 11, 'type' => 'int'),

    ), array('id'));
  }

  public function down()
  {
    \DBUtil::drop_table('posts');
  }
}

變更時間戳記及時間戳記欄位

不管是在 ORM 模型或 CRUD 模型(\Model_Crud),當你使用時間欄位時,你可以選擇你自己的欄位名稱。 使用 --created-at--updated-at 選項去設定你自己的欄位名稱。

預設情況下,當你啟用時間戳記,儲存在 unixtime 中,做为一个整数。 如果你更喜歡 MySQL DATETIME 格式,你可以使用 --mysql-timestamp 選項。

$ php oil g model post title:varchar[50] body:text user_id:int --mysql-timestamp --created-at=my_created

将会給你:

<?php

class Model_Post extends \Orm\Model
{
	protected static $_properties = array(
		'id',
		'title',
		'body',
		'user_id',
		'my_created',
		'updated_at'
	);

	protected static $_observers = array(
		'Orm\Observer_CreatedAt' => array(
			'events' => array('before_insert'),
			'mysql_timestamp' => true,
			'property' => 'my_created',
		),
		'Orm\Observer_UpdatedAt' => array(
			'events' => array('before_save'),
			'mysql_timestamp' => true,
		),
	);
}
<?php

namespace Fuel\Migrations;

class Create_posts
{
	public function up()
	{
		\DBUtil::create_table('posts', array(
			'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
			'title' => array('constraint' => 50, 'type' => 'varchar'),
			'body' => array('type' => 'text'),
			'user_id' => array('constraint' => 11, 'type' => 'int'),
			'my_created' => array('constraint' => 11, 'type' => 'int'),
			'updated_at' => array('constraint' => 11, 'type' => 'int'),

		), array('id'));
	}

	public function down()
	{
		\DBUtil::drop_table('posts');
	}
}

使用 Model_Soft 產生模型

FuelPHP v1.5 添加一个 Model_Soft 为底的 ORM 模型。添加 --soft-delete 以使用 Model_Soft。

$ php oil g model post title:varchar[50] body:text user_id:int --soft-delete

将会給你:

<?php

class Model_Post extends \Orm\Model_Soft
{
	protected static $_properties = array(
		'id',
		'title',
		'body',
		'user_id',
		'created_at',
		'updated_at',
		'deleted_at',
	);

	protected static $_observers = array(
		'Orm\Observer_CreatedAt' => array(
			'events' => array('before_insert'),
			'mysql_timestamp' => false,
		),
		'Orm\Observer_UpdatedAt' => array(
			'events' => array('before_update'),
			'mysql_timestamp' => false,
		),
	);

	protected static $_soft_delete = array(
		'mysql_timestamp' => false,
	);
}
<?php

namespace Fuel\Migrations;

class Create_posts
{
	public function up()
	{
		\DBUtil::create_table('posts', array(
			'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
			'title' => array('constraint' => 50, 'type' => 'varchar'),
			'body' => array('type' => 'text'),
			'user_id' => array('constraint' => 11, 'type' => 'int'),
			'created_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),
			'updated_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),
			'deleted_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),

		), array('id'));
	}

	public function down()
	{
		\DBUtil::drop_table('posts');
	}
}

如果你希望變更 deleted_at 欄位名稱。使用 --deleted-at 選項來設定你自己的欄位名稱。

$ php oil g model post title:varchar[50] body:text user_id:int --soft-delete --deleted-at=mydeleted

使用 Model_Temporal 產生模型

添加 --temporal 來使用 Model_Temporal。

$ php oil g model post title:varchar[50] body:text user_id:int --temporal

它会給你:

<?php

class Model_Post extends \Orm\Model_Temporal
{
	protected static $_properties = array(
		'id',
		'temporal_start',
		'temporal_end',
		'title',
		'body',
		'user_id',
	);


	protected static $_temporal = array(
		'mysql_timestamp' => false,
	);

	protected static $_primary_key = array('id', 'temporal_start', 'temporal_end');
	protected static $_table_name = 'posts';

}
<?php

namespace Fuel\Migrations;

class Create_posts
{
	public function up()
	{
		\DBUtil::create_table('posts', array(
			'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
			'temporal_start' => array('constraint' => 11, 'type' => 'int'),
			'temporal_end' => array('constraint' => 11, 'type' => 'int'),
			'title' => array('constraint' => 50, 'type' => 'varchar'),
			'body' => array('type' => 'text'),
			'user_id' => array('constraint' => 11, 'type' => 'int'),

		), array('id'));
	}

	public function down()
	{
		\DBUtil::drop_table('posts');
	}
}

請注意,temporal_starttemporal_end 不会添加到迁移的 primary_key 陣列。你必須手动添加。

--no-timestamp 預設是設为 true,这意味著 created_atupdated_at 欄位以及相關觀察者都会被忽略。你可以帶 --no-timestamp=0 繞過这个預設让他們回來。

如果你想要變更 temporal_start 或 temporal_end 欄位名稱。使用 --temporal-start--temporal-end 選項來設定你自己的欄位名稱。

$ php oil g model post title:varchar[50] body:text user_id:int --temporal --temporal-start=mystart --temporal-end=myend

使用 Model_Nestedset 產生模型

添加 --nestedset 來使用 Model_Nestedset。

$ php oil g model post title:varchar[50] body:text user_id:int --nestedset

它会給你:

<?php

class Model_Post extends \Orm\Model_Nestedset
{
	protected static $_properties = array(
		'id',
		'left_id',
		'right_id',
		'title',
		'body',
		'user_id',
	);

	protected static $_table_name = 'posts';

}
<?php

namespace Fuel\Migrations;

class Create_posts
{
	public function up()
	{
		\DBUtil::create_table('posts', array(
			'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
			'left_id' => array('constraint' => 11, 'type' => 'int', 'unsigned' => true),
			'right_id' => array('constraint' => 11, 'type' => 'int', 'unsigned' => true),
			'title' => array('constraint' => 50, 'type' => 'varchar'),
			'body' => array('type' => 'text'),
			'user_id' => array('constraint' => 11, 'type' => 'int'),

		), array('id'));
	}

	public function down()
	{
		\DBUtil::drop_table('posts');
	}
}

--no-timestamp 預設是設为 true,这意味著 created_atupdated_at 欄位以及相關觀察者都会被忽略。你可以帶 --no-timestamp=0 繞過这个預設让他們回來。

如果你想要變更 title、tree_id、left_id、right_id 欄位名稱。使用 --title--tre-id--left-id--right-id 選項其中之一來設定你自己的欄位名稱。

$ php oil g model post title:varchar[50] body:text user_id:int --nestedset --title=mytitle --tree-id=mytreeid --left-id=myleftid --right-id=myrightid

表现控件

(選擇性)你可以让 oil 產生檢視伴隨著一个 Presenter 类別。

$ php oil g controller posts action1 action2 action3 --with-presenter

执行迁移

以下命令舉例說明,如何使用提煉命令去运行有用的迁移任務。 假設系统目前在迁移 5,藉由所給參数,迁移任務可以直接移动到一个所給的版本, 或只是 up/down 的单一版本。

$ php oil refine migrate
	Currently on migration: 5.

$ php oil refine migrate --version=4
	Migrated to version: 4.

$ php oil refine migrate --version=5
	Migrated to version: 5.

$ php oil refine migrate:down
	Migrated to version: 4.

$ php oil refine migrate:up
	Migrated to version: 5.

支援以下欄位类型:string[n]varchar[n]int[n]enum[value1, value2]decimal[n, n]float[n, n]textblobdatetimedatetimestamptime

產生迁移

你可以產生迁移而無須建立一个模型,这可以用來重新命名表或添加欄位到表,这是容易佈署在其他环境中的方式。

$ php oil generate migration rename_table_users_to_accounts
	Building magic migration: rename_table
	Created migration: APPPATH/migrations/002_rename_table_users_to_accounts.php

神奇迁移

有一些「神奇」的迁移,它会自动建立一个基於你的迁移名稱前綴的迁移。

$ php oil generate migration create_users name:text email:string[50] password:string[125]
$ php oil generate migration rename_table_users_to_accounts
$ php oil generate migration add_bio_to_accounts bio:text
$ php oil generate migration delete_bio_from_accounts bio:text
$ php oil generate migration rename_field_name_to_username_in_accounts
$ php oil generate migration drop_accounts

請注意:命名你的迁移時要小心,別意外地以任何保留字開頭。

鷹架

鷹架是 Oil 程式码產生中真正令人興奮的部分。这種方法在很大程度上借用了做得很好的 Rails, 这个想法是你不僅建立了 MVC 的骨架和迁移,也将他們與預設的 CRUD 程式码放置在一起, 所以程式码将在撰寫命令後實際运行。

$ php oil g scaffold monkey name:string description:text
	Created model: APPPATH/classes/model/monkey.php
	Created migration: APPPATH/migrations/003_create_monkeys.php
	Created controller: APPPATH/classes/controller/monkeys.php
	Created view: APPPATH/views/monkeys/index.php
	Created view: APPPATH/views/monkeys/view.php
	Created view: APPPATH/views/monkeys/create.php
	Created view: APPPATH/views/monkeys/edit.php
	Created view: APPPATH/views/monkeys/_form.php

$ php oil refine migrate
Migrated to latest version: 3.

正如你可以看到,很多代码是由包含在第二个命令的命令所產生, 控制器看起來像这樣:

class Controller_Monkey extends Controller_Template
{

	public function action_index()
	{
		$data['monkeys'] = Model_Monkey::find('all');
		$this->template->title = "Monkeys";
		$this->template->content = View::forge('monkey/index', $data);

	}

	public function action_view($id = null)
	{
		$data['monkey'] = Model_Monkey::find($id);

		$this->template->title = "Monkey";
		$this->template->content = View::forge('monkey/view', $data);

	}

	public function action_create($id = null)
	{
		if (Input::method() == 'POST')
		{
			$monkey = Model_Monkey::forge(array(
				'name' => Input::post('name'),
				'description' => Input::post('description'),
			));

			if ($monkey and $monkey->save())
			{
				Session::set_flash('success', 'Added monkey #'.$monkey->id.'.');

				Response::redirect('monkey');
			}

			else
			{
				Session::set_flash('error', 'Could not save monkey.');
			}
		}

		$this->template->title = "Monkeys";
		$this->template->content = View::forge('monkey/create');

	}

	public function action_edit($id = null)
	{
		$monkey = Model_Monkey::find($id);

		if (Input::method() == 'POST')
		{
			$monkey->name = Input::post('name');
			$monkey->description = Input::post('description');

			if ($monkey->save())
			{
				Session::set_flash('success', 'Updated monkey #' . $id);

				Response::redirect('monkey');
			}

			else
			{
				Session::set_flash('error', 'Could not update monkey #' . $id);
			}
		}

		else
		{
			$this->template->set_global('monkey', $monkey, false);
		}

		$this->template->title = "Monkeys";
		$this->template->content = View::forge('monkey/edit');

	}

	public function action_delete($id = null)
	{
		if ($monkey = Model_Monkey::find($id))
		{
			$monkey->delete();

			Session::set_flash('success', 'Deleted monkey #'.$id);
		}

		else
		{
			Session::set_flash('error', 'Could not delete monkey #'.$id);
		}

		Response::redirect('monkey');

	}


}

admin 鷹架

你可以用 admin 交換鷹架并產生一个擴充 Controller_Admin 而不是 Controller_Template 的控制器。 在第一次使用此命令,一个管理骨架会被產生,要擴充此骨架使用強制跳過參数。 要在子目录中產生,命名前綴相應的模型名稱。

$ php oil g admin project_entry title:string abstract:text full_text:text project_id:int is_draft:int order:int -s
	Creating migration: APPPATH/migrations/012_create_project_entries.php
	Creating model: APPPATH/classes/model/project/entry.php
	Creating controller: APPPATH/classes/controller/admin/project/entry.php
	Creating view: APPPATH/views/admin/project/entry/index.php
	Creating view: APPPATH/views/admin/project/entry/view.php
	Creating view: APPPATH/views/admin/project/entry/create.php
	Creating view: APPPATH/views/admin/project/entry/edit.php
	Creating view: APPPATH/views/admin/project/entry/_form.php

任務

你也可以让 oil 產生一个新任務的骨架。

$ php oil g task newtask cmd1 cmd2

将產生

<?php

namespace Fuel\Tasks;

class Newtask
{
	public static function run($args = NULL)
	{
		echo "\n===========================================";
		echo "\nRunning DEFAULT task [Newtask:Run]";
		echo "\n-------------------------------------------\n\n";

		/***************************
		 Put in TASK DETAILS HERE
		 **************************/
	}

	public static function cmd1($args = NULL)
	{
		echo "\n===========================================";
		echo "\nRunning task [Newtask:Cmd1]";
		echo "\n-------------------------------------------\n\n";

		/***************************
		 Put in TASK DETAILS HERE
		 **************************/
	}

	public static function cmd2($args = NULL)
	{
		echo "\n===========================================";
		echo "\nRunning task [Newtask:Cmd2]";
		echo "\n-------------------------------------------\n\n";

		/***************************
		 Put in TASK DETAILS HERE
		 **************************/
	}
}
/* End of file tasks/newtask.php */

配置

要產生一个 配置,使用以下命令:

$ php oil g config sample hello:world
	Created config: APPPATH/config/sample.php

这将產生一个配置看起來像这樣:

return array (
	'hello' => 'world',
);

/* End of file sample.php */

從 COREPATH 產生配置

從 COREPATH/config 合併配置,如果 APPPATH/config 不存在

$ php oil g config package
	Created config: APPPATH/config/package.php

这将產生一个配置看起來像这樣:

return array (
	'sources' =>
	array (
		0 => 'github.com/fuel-packages',
	),
);

從 COREPATH & APPPATH 強制更新配置

從 COREPATH/config 及 APPPATH/config 合併配置到 APPPATH/config

$ php oil g config form --overwrite
	Created config: APPPATH/config/form.php

这将產生一个配置看起來像这樣:

return array (
	'prep_value' => true,
	'auto_id' => true,
	'auto_id_prefix' => '',
	'form_method' => 'post',
);

/* End of file form.php */

套件

要產生一个 套件,使用以下命令:

$ php oil g package sample
	Creating file: PKGPATH/sample/classes/sample.php
	Creating file: PKGPATH/sample/config/sample.php
	Creating file: PKGPATH/sample/bootstrap.php

產生套件的路徑預設是 PKGPATH,但是这个值可以透過傳遞 --path=package_path-p=package_path 選項到命令來配置 package_paths,以變更为定義在其中的任何路徑。

產生驅动为底的套件

如果你希望產生一个驅动为底的套件,简单地提供 --drivers-d 選項:

$ php oil g package sample -d
	Creating file: PKGPATH/sample/classes/sample.php
	Creating file: PKGPATH/sample/classes/sample/driver.php
	Creating file: PKGPATH/sample/config/sample.php
	Creating file: PKGPATH/sample/bootstrap.php

你也可以產生你自己的驅动。简单地以逗號分隔傳遞驅动名稱到 --drivers-d 選項:

$ php oil g package sample -d=driver1,driver2
	Creating file: PKGPATH/sample/classes/sample.php
	Creating file: PKGPATH/sample/classes/sample/driver.php
	Creating file: PKGPATH/sample/classes/sample/driver1.php
	Creating file: PKGPATH/sample/classes/sample/driver2.php
	Creating file: PKGPATH/sample/config/sample.php
	Creating file: PKGPATH/sample/bootstrap.php

產生帶 VCS 档案的套件

如果你希望为你的套件產生 composer.json 和 README.md 档案,简单地提供 --vcs-v 選項:

$ php oil g package sample -v
	Creating file: PKGPATH/sample/composer.json
	Creating file: PKGPATH/sample/README.md
	Creating file: PKGPATH/sample/classes/sample.php
	Creating file: PKGPATH/sample/config/sample.php
	Creating file: PKGPATH/sample/bootstrap.php

模組

要產生一个 模組,使用以下命令:

$ php oil g module blog

此命令会在你应用程序定義在 config.module_paths 中的模組路徑建立一个名为 blog 的文件夾。如果你定義了多个模組路徑,你会得到一个你可以選擇的路徑列表,如:

$ php oil g module blog
Your app has multiple module paths defined. Please choose the appropriate path from the list below
[1] /var/www/fuel/shared/modules/
[2] /var/www/fuel/app/modules/

为了更简单提供模組產生,你可以提供一个以逗號分隔要被建立文件夾的列表給 --folders 選項。这些可以被近乎無限地嵌套,而且你不需要提供每个父文件夾。一个简短但很有用的範例可以是:

$ php oil g module blog --folders=classes/model,classes/controller,config,lang