本文介绍了如何使用 PHP 以及可用的模型与 Tailor 交互。
此 Tailor\Models\EntryRecord 模型用于存储条目的内容。
除了您定义的表单字段,以下属性可以在检索到的模型上找到。
| Attribute | Description |
|---|---|
| id | The Primary Key found in the database. |
| blueprint_uuid | The UUID of the associated blueprint. |
| content_group | The content group name, if used. |
| title | A title descriptor for the entry, for example, My Blog Post. |
| slug | A slug identifier for the entry, for example, my-blog-post. |
| is_enabled | Determines if the entry is currently visible. |
| created_at | The creation date for the entry. |
| updated_at | The last updated date for the entry. |
| expired_at | The expiry date for the entry. |
| published_at | The published date for the entry. |
| published_at_date | The published date, or if none is specified, the creation date. |
如果一个条目类型是 structure,它将具有一些额外属性。
| Attribute | Description |
|---|---|
| fullslug | A slug identifier that includes parent slugs, for example, parent-slug/child-slug. |
| parent | The parent record for this entry, if available. |
| children | The child records for this entry, if available. |
如果条目类型是 stream,它将有一些额外的属性。
| Attribute | Description |
|---|---|
| published_at_day | The numerical day the entry was published. |
| published_at_month | The numerical month the entry was published. |
| published_at_year | The numerical year the entry was published. |
要使用 PHP 处理条目,您可以使用 Tailor\Models\EntryRecord 模型并调用 inSection 静态方法,传入句柄以返回一个准备好的 数据库模型查询。或者,您可以使用 UUID 和 inSectionUuid 方法查找它。
以下 get 方法查询返回 一组记录。
$records = EntryRecord::inSection('Blog\Post')->get();
$records = EntryRecord::inSectionUuid('a63fabaf-7c0b-4c74-b36f-7abf1a3ad1c1')->get();结合 where 约束,你可以使用 first 方法找到单个记录。以下代码将找到 slug 等于 first-post 的条目。
$record = EntryRecord::inSection('Blog\Post')->where('slug', 'first-post')->first();如果一个 条目类型 被设置为 single,你可以使用 findSingleForSection 方法来查找该条目。同样地,findSingleForSectionUuid 可以用于通过 UUID 进行查找。这些方法将确保查找时记录存在。
$record = EntryRecord::findSingleForSection('Homepage');
$record = EntryRecord::findSingleForSectionUuid('3328c303-7989-462e-b866-27e7037ba275');inSection 方法可用于动态创建记录。以下将创建一个新的博客文章条目。相同的代码在首次检索到现有记录后也可用于更新该记录。
$post = EntryRecord::inSection('Blog\Post');
$post->title = 'Imported Post';
$post->save();该 Tailor\Models\GlobalRecord 模型用于存储一个全局对象的内容。
除了已定义的表单字段之外,在检索到的模型上可以找到以下属性。
| Attribute | Description |
|---|---|
| id | The Primary Key found in the database. |
| blueprint_uuid | The UUID of the associated blueprint. |
要使用 PHP 查找一个全局项,你可以使用 Tailor\Models\GlobalRecord 模型并调用 findForGlobal 静态方法,传入其句柄。或者,你可以使用 UUID 和 findForGlobalUuid 方法来查找它。
GlobalRecord::findForGlobal('Blog\Config');
GlobalRecord::findForGlobalUuid('7b193500-ac0b-481f-a79c-2a362646364d');相关字段可以包括复用器字段和条目字段,并且读取和写入这些字段需要一些额外步骤。
读取关联字段时,您可以使用load方法在集合上预加载它们。此方法使关联内容作为单个查询可用,并且对性能最有利。
以下急切加载 categories 字段并将其添加到结果中,以及一个传递多个相关字段的示例。
$records->load('categories');
$records->load(['categories', 'author']);当编写关联字段时,您可以将关联名称作为方法调用以返回关联定义,然后可以调用后续的 create() 方法,该方法将返回新创建的关联。
以下找到 Blog\Post 部分中的第一个博客文章,然后创建一个关联类别。
$post = EntryRecord::inSection('Blog\Post')->first();
$post->categories()->create(['title' => 'Test', 'price' => '100']);使用 make() 方法来创建一个新的空模型实例。
$category = $post->categories()->make();如果类别已存在,请使用 add() 方法。以下将第一个博客类别添加到第一个博客文章。
$post = EntryRecord::inSection('Blog\Post')->first();
$category = EntryRecord::inSection('Blog\Category')->first();
$post->categories()->add($category);查看 关联文章 了解有关模型关系的更多信息。
类似于扩展常规模型,您可以使用EntryRecord模型构造函数来扩展extendInSection方法,以针对特定的蓝图。此外,extendInSectionUuid方法也可用于更精确的定位。
EntryRecord::extendInSection('Blog\Post', function($model) {
$model->bindEvent('model.afterDelete', function () use ($model) {
// Model has been deleted!
});
});该 GlobalRecord 模型构造函数也支持扩展使用 extendInGlobal 和 extendInGlobalUuid 方法来针对特定的蓝图。
GlobalRecord::extendInGlobal('Blog\Config', function($model) {
$model->bindEvent('model.beforeSave', function () use ($model) {
// Model has been saved!
});
});方法
extendInSectionUuid和extendInGlobalUuid不会抛出异常,如果蓝图未找到。
在某些情况下,您可能希望将定制模型与常规数据库模型结合使用。
recordfinder表单字段将关系定义引入常规模型,例如由插件定义的模型。modelClass应引用模型类,并且list属性对于单数关系是必需的,由maxItems指定。
products:
label: Products
type: recordfinder
modelClass: Acme\Test\Models\Product
list: $/acme/test/models/product/columns.yaml
maxItems: 1了解有关 记录查找器表单组件 此处。
由于所有 Tailor 模型共享相同的模型类,你的关系定义中需要一些额外的属性。Tailor\Traits\BlueprintRelationModel 实现了这些属性,以引用 Tailor 模型,支持属于 (Belongs To) 和 多对多属于 (Belongs To Many) 关系。
当特性 BlueprintRelationModel 在您的模型中实现时,您可以提供一个 blueprint 属性,以及引用 Tailor 蓝图的蓝图 UUID。以下将与 Tailor\Models\EntryRecord 类建立一个名为 author 的 Belongs To 关系。
class Product extends Model
{
use \Tailor\Traits\BlueprintRelationModel;
public $belongsTo = [
'author' => [
\Tailor\Models\EntryRecord::class,
'blueprint' => '6947ff28-b660-47d7-9240-24ca6d58aeae'
]
];
}您也可以使用实现特定的字段类型来引用常规模型,例如,客户内容字段可以硬编码到 Customer 模型类。这涉及到创建一个自定义定制字段,该字段将提供对模型和数据库表的完全访问。
在内容字段类定义中,extendModelObject 方法允许内容字段扩展记录模型,以及 extendDatabaseTable 以向数据库表添加列。
class MyContentField extends ContentFieldBase
{
public function extendModelObject($model)
{
$model->belongsTo[$this->fieldName] = MyOtherModel::class;
}
public function extendDatabaseTable($table)
{
$table->integer($this->fieldName . '_id')->nullable();
}
}这需要多付出一点努力才能让事情正常运作,但结果是一个简单的字段类型定义,只需要最少的配置。
myfield:
label: My Field
type: mycontentfield了解更多关于构建自定义 Tailor 字段。