romano83/cakephp3-draft

此包已被弃用,不再维护。未建议替换包。

此草案插件使您能够快速为您的模型创建草稿系统。

安装: 178

依赖项: 0

建议者: 0

安全性: 0

星标: 3

关注者: 2

分支: 1

开放问题: 4

类型:cakephp-plugin

v1.0 2015-08-01 11:38 UTC

This package is auto-updated.

Last update: 2022-03-11 03:06:42 UTC


README

此草案插件使您能够快速为您的模型创建草稿系统。

此插件是 CakePHP 3.* 的Grafikart's Cakephp-Draft 插件

Build Status Coverage Status Latest Stable Version License

安装

要求

安装步骤

  • 运行
composer require romano83/cakephp3-draft:1.*

如何使用

在您的 config\bootstrap.php 文件中,添加以下行

Plugin::load('Romano83/cakephp3-draft');

或者

Plugin::loadAll();

插件现在已加载,您可以在表中添加 DraftBehavior

<?php
namespace MyApp\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table
{
	public function initialize(array $config)
	{
		$this->addBehavior('Romano83/Cakephp3Draft.Draft');
	}
}

默认情况下,插件使用“online”字段来设置内容的状体。

  • online = -1 时内容为草稿
  • online = 0 时内容为离线
  • online = 1 时内容为在线

自定义

如果您想使用自定义字段,可以覆盖默认行为配置

<?php
namespace MyApp\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table
{
	public function initialize(array $config)
	{
		$this->addBehavior(
			'Romano83/Cakephp3Draft.Draft', [
				'conditions' => [
					'draft' => 1
				]
			]
		);
	}
}

例如,此配置将使用“draft”字段(设置为 1)来创建草稿。

方法

附加插件后,模型将具有新的方法 getDraftId(Table $table, array $conditions = []),该方法返回草稿 ID。第一个参数是 \Cake\ORM\Table 实例,第二个参数是可选的数组。

如何实现此方法

以下是在您的 PostsController 中使用此方法的示例

public function add(){
	$post = $this->Posts->newEntity();
	if($this->request->is(['post', 'put'])){
		// Do your stuff here...
	}else{
		$post->id = $this->Posts->getDraftId($this->Posts); // get the last draft Id or create new one if needed
	}
}

// OR
public function add(){
	$post = $this->Posts->newEntity();
	if($this->request->is(['post', 'put'])){
		// Do your stuff here...
	}else{
		$post->id = $this->Posts->getDraftId($this->Posts, ['user_id' => 2]); // Get a draft Id for a content belonging to user 2 (or create a new one)
	}
}

如果您想清理您的表中的草稿,您可以实现此方法

$this->Posts->cleanDrafts($this->Posts);

如何贡献

  • 如果您发现了错误,请在 GitHub 上创建一个工单。
  • 如果您想提交 PR,请创建一个新分支。
  • 您的代码 必须 遵循CakePHP 编码规范
  • 如果您需要,您 必须 添加测试用例。

特别感谢