当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

產生 - Oil 套件

优质
小牛编辑
117浏览
2023-12-01

产生

程式码产生可用于建立许多重複的程式码为你加速开发时间, 这完全是选择性的 - 就像所有的 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