o-log / php-crud
CRUD 表格 + 表单
Requires
- php: >=7.2
- ext-mbstring: *
- o-log/php-bt: 9.*
- o-log/php-model: 10.*
Requires (Dev)
- o-log/php-router: 2.*
- phpunit/phpunit: 4.8.*
- dev-master
- 9.17
- 9.16
- 9.15
- 9.14
- 9.13
- 9.12
- 9.11
- 9.10
- 9.9
- 9.8
- 9.7
- 9.6
- 9.5
- 9.4
- 9.3
- 9.2
- 9.1
- 9.0
- 8.0
- 7.12
- 7.11
- 7.10
- 7.9
- 7.8
- 7.7
- 7.6
- 7.5
- 7.4
- 7.3
- 7.2
- 7.1
- 7.0
- 6.0
- 5.5
- 5.4
- 5.3
- 5.2
- 5.1
- 5.0
- 4.46
- 4.45
- 4.44
- 4.43
- 4.42
- 4.41
- 4.40
- 4.39
- 4.38
- 4.37
- 4.36
- 4.35
- 4.34
- 4.33
- 4.32
- 4.31
- 4.30
- 4.29
- 4.28
- 4.27
- 4.26
- 4.25
- 4.24
- 4.23
- 4.22
- 4.21
- 4.20
- 4.19
- 4.18
- 4.17
- 4.16
- 4.15
- 4.14
- 4.13
- 4.12
- 4.11
- 4.10
- 4.9
- 4.8
- 4.7
- 4.6
- 4.5
- 4.4
- 4.3
- 4.2
- 4.1
- 4.0
- 3.42
- 3.41
- 3.40
- 3.39
- 3.38
- 3.37
- 3.36
- 3.35
- 3.34
- 3.33
- 3.32
- 3.31
- 3.30
- 3.29
- 3.28
- 3.27
- 3.26
- 3.25
- 3.24
- 3.23
- 3.22
- 3.21
- 3.20
- 3.19
- 3.18
- 3.17
- 3.16
- 3.15
- 3.14
- 3.13
- 3.12
- 3.11
- 3.10
- 3.9
- 3.8
- 3.7
- 3.6
- 3.5
- 3.4
- 3.3
- 3.2
- 3.1
- 3.0
- 2.16
- 2.15
- 2.14
- 2.13
- 2.12
- 2.11
- 2.10
- 2.9
- 2.8
- 2.7
- 2.6
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0
- 1.1
- 1.0
- 0.5
- 0.4
- 0.3
- 0.2
- dev-feature/SBOR-438/date_range
- dev-paa_42
- dev-paa_41
- dev-paa_40
- dev-olog_table_editor
- dev-mk-fix
- dev-mk-table-filter-not-equal
- dev-mk-ref-select-widget
- dev-table-widget-html
- dev-t-params-by-default
- dev-t-fix-null
- dev-crud-form
- dev-table-options
- dev-timestamp-table-widget
- dev-mk-progress-bar-widget
This package is auto-updated.
Last update: 2024-09-18 03:02:15 UTC
README
该库能做两件事:输出对象列表和输出单个对象的编辑表单。它生成的是html表格或表单的代码,而不是完整的页面。生成的html代码与twitter bootstrap兼容。
其他,如权限检查,将表格或表单输出到页面中的指定位置,需要单独处理。可以使用现成的模块来完成。
- php-auth: 用户认证和权限检查
- php-bt: 包含页面模板
对象通过 \OLOG\Model\InterfaceFactory 方法从数据库加载,因此对象类必须实现此接口。
对象通过 \OLOG\Model\InterfaceSave 方法保存到数据库,因此对象类必须实现此接口以进行编辑。
安装演示项目
在要部署演示项目的文件夹中,在控制台中执行以下命令:
git clone https://github.com/o-log/php-crud.git
cd php-crud
composer update
之后需要创建一个空的数据库项目,并在 CRUDDemo/Config.php 文件中指定对其的访问参数。
现在创建数据库中的表格,为此执行数据库结构的迁移脚本。
php cli.php
选择第1项,然后回答脚本的提问。
创建表格后,启动本地服务器并检查其工作情况。
./run.sh
在浏览器中打开地址 localhost:8000
将库连接到项目
在 composer.json 项目中包含以下行
"require" : {
"o-log/php-crud" : "dev-master"
}
输出对象列表
以下是一个生成html表格对象的代码示例
$html .= CRUDTable::html(
DemoNode::class,
\OLOG\CRUD\CRUDForm::html(
new DemoNode(),
[
new CRUDFormRow(
'Title',
new CRUDFormWidgetInput('title')
)
]
),
[
new CRUDTableColumn(
'Edit',
new CRUDTableWidgetText('{this->title}')
),
new CRUDTableColumn(
'Reverse title',
new CRUDTableWidgetText('{this->getReverseTitle()}')
),
new CRUDTableColumn(
'Edit',
new CRUDTableWidgetTextWithLink(
'{this->title}',
DemoNodeEditAction::getUrl('{this->id}')
)
),
new CRUDTableColumn(
'Weight',
new CRUDTableWidgetWeight(
[
'parent_id' => '{this->parent_id}'
]
)
),
new CRUDTableColumn(
'Delete',
new CRUDTableWidgetDelete()
),
],
[],
'title'
);
在表格中输出数据
调用表格小部件时,我们传递给它一个字符串:要输出什么。在这个字符串中,可以访问当前表格行中显示的对象的字段和方法,也可以访问其他对象。
当前表格行中的对象可以通过关键词 this 访问。
输出字段和调用当前对象的方法的示例
new CRUDTableWidgetText('{this->title}') new CRUDTableWidgetText('{this->getReverseTitle()}') new CRUDTableWidgetHtml('{this->id}
{this->title}')
可以通过以下方式访问当前对象引用的其他对象
new CRUDTableWidgetText('{DemoTerm.{this->term_id}->title}')
这里我们输出与新闻相关的分类名称:创建一个具有标识符的 DemoTerm 类对象,该标识符是从当前对象的 term_id 字段中获取的,并输出其名称。
在实际应用中,建议使用自动常量而不是标量来指定类名,因此最终的记录将如下所示
new CRUDTableWidgetText('{' . DemoTerm::class . '.{this->term_id}->title}')
有时在权重小部件中需要考虑用于计算表格中对象权重的额外参数。为此,我们传递一个上下文字符串数组
例如
new CRUDTableWidgetWeight([ 'parent_id' => '{this->parent_id}' ])
在这种情况下,处理权重更改时,将使用与当前对象相同的 parent_id 计算记录的权重
输出对象编辑器
以下是一个生成html对象编辑器的代码示例
$node_obj = DemoNode::factory($node_id);
$html .= \OLOG\CRUD\CRUDForm::html(
$node_obj,
[
new CRUDFormRow(
'Id',
new CRUDFormWidgetInput('id')
),
new CRUDFormRow(
'Title',
new CRUDFormWidgetTextarea('title')
),
new CRUDFormRow(
'image_path_in_images nullable',
new CRUDFormWidgetInput('image_path_in_images', true)
),
new CRUDFormRow(
'Date',
new CRUDFormWidgetTimestamp('created_at_ts')
),
new CRUDFormRow(
'is_published',
new CRUDFormWidgetRadios('is_published', [0 => 'no', 1 => 'yes'])
),
new CRUDFormRow(
'published_at_datetime_str',
new CRUDFormWidgetDateTime('published_at_datetime_str')
),
new CRUDFormRow(
'expiration_date nullable',
new CRUDFormWidgetDate('expiration_date')
),
new CRUDFormRow(
'State code',
new CRUDFormWidgetOptions('state_code',
[
1 => 'announce',
2 => 'live',
3 => 'archive'
]
)
),
new CRUDFormRow(
'State code',
new CRUDFormWidgetHtml('<ul><li>html widget - line 1</li><li>html widget - line 2</li></ul>')
),
new CRUDFormVerticalRow(
'Body',
new CRUDFormWidgetAceTextarea('body')
)
]
);