aiphee/related-content

此包的最新版本(0.0.3)没有可用的许可信息。

CakePHP的RelatedContent插件

安装: 11

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

公开问题: 1

类型:cakephp-plugin

0.0.3 2016-02-11 14:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:42:04 UTC


README

Build Status

CakePHP 3的相关内容插件

Autocomplete Related view

这是一个快速制作的插件,用于简单维护关系。

您可以为表中的一条记录定义在其它表中存在类似记录。

它还包含依赖于Bootstrap 3的基本元素。

请注意,这不是一个复杂的插件,可能存在严重错误,欢迎报告或修复。

安装

  • 从插件运行迁移或手动创建表。
CREATE TABLE `related_contents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `source_table_name` varchar(255) DEFAULT NULL COMMENT 'Table which you link from',
  `target_table_name` varchar(255) DEFAULT NULL COMMENT 'Table which you link to',
  `source_table_id` int(11) NOT NULL COMMENT 'ID of table you link from',
  `target_table_id` int(11) NOT NULL COMMENT 'ID of table you link to',
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
)
  • 将仓库内容复制到插件目录,并添加到您的cakephp composer.json中
    "autoload": {
        "psr-4": {
            "App\\": "src",
            "RelatedContent\\": "./plugins/RelatedContent/src",
        }
    }

或使用packagist composer require aiphee/related-content

  • 添加到您的 bootstrap.php
Plugin::load('RelatedContent', ['routes' => true]);

用法

  • 为您的表添加第一个行为以便可搜索(添加到缓存中进行搜索)
$this->addBehavior('RelatedContent.InRelatedIndex');
  • 添加第二个具有“相似”的行为
$this->addBehavior('RelatedContent.HasRelated', isset($config['options']) ? $config['options'] : []);
  • 将元素添加到您的视图中
<?= $this->element('RelatedContent.managingRelated', ['tables_to_get' => ['ContentNews', 'ContentPages']]) ?>

参数 tables_to_get 是可选的,它将允许只在某些表中搜索。

  • 要获取与您的条目相关的相关内容,您必须传递给find的参数
$entity = $this->ContentPages->get($id, [
				'getRelated' => true
			]);
  • 您还可以在视图中使用元素,该模型没有HasRelated行为,以填充一些字段(target_table_name和target_table_id直接在表中)
<?= $this->element('RelatedContent.foreignTableSearch') ?>
<?= $this->Form->input('foreign_table_id', ['type' => 'hidden']) ?>
<?= $this->Form->input('foreign_table_name', ['type' => 'hidden']) ?>

它看起来像这样

Managing related without associations