rsanchez/deep

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

ExpressionEngine Channel Entries 的 Eloquent 模型集。

3.0.1 2021-01-27 19:14 UTC

README

Build Status Total Downloads Latest Stable Version

这是一个只读的 ExpressionEngine Channel Entries 的 Eloquent 模型集。此库有几个目标

  • 尽可能使用 Eloquent 查询作用域(query scopes)来复制 {exp:channel:entries} 的功能
  • 与标准 Eloquent 模型方法链式调用(例如 ->where('foo', 'bar')
  • 使用预加载最小化所需查询的数量
  • 提供一个基础插件,EE 插件/模块可以扩展,其功能与 {exp:channel:entries} 几乎相同
  • 使用字段名和实体自动获取自定义字段,而不是仅从 exp_channel_data 中获取原始文本

有关更详细的信息,请参阅 自动生成的 API 文档

版本兼容性图表

<?php

use rsanchez\Deep\Deep;
use rsanchez\Deep\Model\Entry;

Deep::bootInstance();

$entries = Entry::channel('blog')
                ->limit(10)
                ->showFutureEntries()
                ->get();
?>

<?php foreach ($entries as $entry) : ?>
<article>
    <h1><?php echo e($entry->title); ?></h1>

    <p class="date"><?php echo $entry->entry_date->format('F j, Y'); ?></p>

    <?php echo $entry->description; ?>
</article>
<?php endforeach ?>

安装

在您的终端运行此命令

composer require rsanchez/deep

设置

ExpressionEngine

确保您在 config.php 的顶部加载 composer 的自动加载器(您的实际供应商路径可能不同)

require_once FCPATH.'vendor/autoload.php'

然后您可以通过扩展 BasePlugin 类来创建自己的插件,该插件使用 Deep。或者您可以使用内置的包装类,该类为您启动 Deep 和 EE

use rsanchez\Deep\Deep;
use rsanchez\Deep\Model\Entry;

Deep::bootEE();

$entries = Entry::channel('blog')
                ->limit(10)
                ->get();

Laravel

Deep 附带了一个 Laravel 服务提供程序。将其添加到 app/config/app.php 中的提供程序列表

'rsanchez\Deep\App\Laravel\ServiceProvider',

use rsanchez\Deep\Model\Entry;

route('/blog', function()
{
    $entries = Entry::channel('blog')->get();
    return View::make('blog.index')->withEntries($entries);
});

route('/blog/json', function()
{
    $entries = Entry::channel('blog')->get();
    return Response::json($entries);
});

如果您正在使用数据库表的前缀(EE 默认使用 exp_,所以您很可能正在使用),请确保在 Laravel 的 app/config/database.php 中设置前缀

如果您需要使用除 Laravel 默认连接以外的数据库连接,您应在 app/config/database.php 中添加以下配置

'deep' => array(
    'connection' => 'your_connection_name',
),

指定的连接将用于 Deep 的所有模型。

通用 PHP(或其他框架)

首先,您必须在 Laravel 之外使用 Eloquent。有关如何做到这一点的许多指南文章其他信息

然后您可以直接使用通用包装器

use rsanchez\Deep\Deep;
use rsanchez\Deep\Model\Entry;

Deep::bootInstance();

$entries = Entry::channel('blog')
                ->limit(10)
                ->get();

或者如果您喜欢,可以实例化自己的 Deep DI 容器实例

use rsanchez\Deep\Deep;

$deep = new Deep();

$entries = $deep->make('Entry')
                ->channel('blog')
                ->limit(10)
                ->get();

使用 Phar 归档以简化分发

您可以使用 Phar 归档作为替代安装方法。最好的方法是将 Deep 与您的自定义分发附加组件打包在一起,使用 Phar 归档,因为 EE 并未原生支持默认的 composer 安装。

要构建 Phar 归档,您必须已安装 box。然后您可以克隆此存储库,运行 composer install 以获取所有依赖项,然后运行 box build 以创建 Phar 归档。构建完成后,归档将位于 build/deep.phar 中。

现在您可以将单个 Phar 归档与您的附加组件打包在一起(例如,在附加组件根目录的 phar 文件夹中),并按如下方式加载:

// this is a courtesy check in case other add-ons are also
// using deep.phar
if ( ! class_exists('\\rsanchez\\Deep\\Deep'))
{
    require_once PATH_THIRD.'your_addon/phar/deep.phar';
}

查询作用域

过滤作用域

过滤范围应该看起来很熟悉,因为它们大多数都与本地的 {exp:channel:entries} 参数相关。

频道名称

Entry::channel('blog', 'news')->get();

非频道名称

Entry::notChannel('blog', 'news')->get();

频道ID

Entry::channelId(1, 2)->get();

非频道ID

Entry::notChannelId(1, 2)->get();

作者ID

Entry::authorId(1, 2)->get();

非作者ID

Entry::notAuthorId(1, 2)->get();

分类ID

Entry::category(1, 2)->get();

非分类ID

Entry::notCategory(1, 2)->get();

所有分类

只显示具有所有指定分类的条目。

Entry::allCategories(1, 2)->get();

非所有分类

排除具有所有指定分类的条目。

Entry::notAllCategories(1, 2)->get();

分类名称

Entry::categoryName('mammals', 'reptiles')->get();

非分类名称

Entry::notCategoryName('mammals', 'reptiles')->get();

分类组

Entry::categoryGroup(1, 2)->get();

非分类组

Entry::notCategoryGroup(1, 2)->get();

Entry::day(31)->get();

动态参数

Entry::dynamicParameters(array('limit', 'search:your_field_name'), $_REQUEST)->get();

条目ID

Entry::entryId(1, 2)->get();

非条目ID

Entry::notEntryId(1, 2)->get();

条目ID从

Entry::entryIdFrom(1)->get();

条目ID到

Entry::entryIdTo(100)->get();

固定排序

Entry::fixedOrder(4, 8, 15, 16, 23, 42)->get();

成员组ID

Entry::groupId(1, 2)->get();

非成员组ID

Entry::notGroupId(1, 2)->get();

限制

Entry::limit(1)->get();

Entry::month(12)->get();

偏移量

Entry::offset(1)->get();

显示已过期

Entry::showExpired(false)->get();

显示未来条目

Entry::showFutureEntries(true)->get();

显示页面

Entry::showPages(false)->get();

仅显示页面

Entry::showPagesOnly(true)->get();

站点ID

Entry::siteId(1, 2)->get();

开始于

Unix时间

Entry::startOn(1394393247)->get();

或使用一个DateTime对象

$date = new DateTime();
Entry::startOn($date)->get();

停止在

Unix时间

Entry::stopBefore(1394393247)->get();

或使用一个DateTime对象

$date = new DateTime();
Entry::stopBefore($date)->get();

粘性

Entry::sticky(true)->get();

状态

Entry::status('open', 'closed')->get();

非状态

Entry::notStatus('open', 'closed')->get();

URL标题

Entry::urlTitle('cats', 'dogs')->get();

非URL标题

Entry::notUrlTitle('cats', 'dogs')->get();

用户名

Entry::username('john_doe', 'jane_doe')->get();

非用户名

Entry::notUsername('john_doe', 'jane_doe')->get();

Entry::year(2014)->get();

tagparams

此范围接受参数数组并将所有支持的 参数 应用到查询中。

Entry::tagparams(ee()->TMPL->tagparams)->get();

以下 tagparams 范围未实现 channel:entries 参数

  • 缓存
  • display_by
  • disable
  • dynamic
  • dynamic_start
  • month_limit
  • paginate
  • paginate_base
  • paginate_type
  • refresh
  • related_categories_mode
  • relaxed_categories
  • require_entry
  • show_current_week
  • track_views
  • week_sort
  • uncategorized_entries

预加载范围

这些范围强制预加载某些关系。使用 Entry 模型(以及 Entries 代理)自动预加载数据字段。使用 Title 模型(或 Titles 代理) 预加载数据字段。

与分类

预加载数据字段 categories

Entry::withCategories()->get();

与分类字段

预加载数据字段 categories 以及自定义分类字段。

Entry::withCategoryFields()->get();

与作者

预加载数据字段 author

Entry::withAuthor()->get();

与作者字段

预加载数据字段 author 以及自定义成员字段。

Entry::withAuthorFields()->get();

与父级

预加载数据字段 parents(仅原生EE关系字段)。

Entry::withParents()->get();

与兄弟

预加载数据字段 siblings(仅原生EE关系字段)。

Entry::withSiblings()->get();

与评论

预加载数据字段 comments,一组评论模型。

Entry::withComments()->get();

无字段

不加载自定义字段。

Entry::withoutFields()->get();

无子字段

不加载子(Playa/关系)条目中的自定义字段。

Entry::withoutChildFields()->get();

有字段

指定要加载的确切自定义字段。

Entry::withFields(['your_custom_field', 'your_other_custom_field'])->get();

自定义字段范围

这组范围允许您使用传统的Eloquent方法,而不是使用 field_id_X,来使用自定义字段名称。

按字段排序

Entry::orderByField('your_custom_field', 'asc')->get();

字段条件

Entry::whereField('your_custom_field', 'foo')->get();

或字段条件

Entry::orWhereField('your_custom_field', 'foo')->get();

字段在...中

Entry::whereFieldIn('your_custom_field', array('foo', 'bar'))->get();

或字段在...中

Entry::orWhereFieldIn('your_custom_field', array('foo', 'bar'))->get();

字段不在...中

Entry::whereFieldNotIn('your_custom_field', array('foo', 'bar'))->get();

或字段不在...中

Entry::orWhereFieldNotIn('your_custom_field', array('foo', 'bar'))->get();

字段介于...

Entry::whereFieldBetween('your_custom_field', array(1, 10))->get();

或字段介于...

Entry::orWhereFieldBetween('your_custom_field', array(1, 10))->get();

字段不在...

Entry::whereFieldNotBetween('your_custom_field', array(1, 10))->get();

或字段不在...

Entry::orWhereFieldNotBetween('your_custom_field', array(1, 10))->get();

字段为空

Entry::whereFieldNull('your_custom_field')->get();

或字段为空

Entry::orWhereFieldNull('your_custom_field')->get();

字段不为空

Entry::whereFieldNotNull('your_custom_field')->get();

或字段不为空

Entry::orWhereFieldNotNull('your_custom_field')->get();

字段包含

类似于 search:your_custom_field="foo|bar"

Entry::whereFieldContains('your_custom_field', 'foo', 'bar')->get();

或字段包含

Entry::orWhereFieldContains('your_custom_field', 'foo', 'bar')->get();

字段不包含

类似于 search:your_custom_field="not foo|bar"

Entry::whereFieldDoesNotContain('your_custom_field', 'foo', 'bar')->get();

或字段不包含

Entry::orWhereFieldDoesNotContain('your_custom_field', 'foo', 'bar')->get();

字段包含整个单词

类似于 search:your_custom_field="foo\W|bar\W"

Entry::whereFieldContainsWholeWord('your_custom_field', 'foo', 'bar')->get();

或字段包含整个单词

Entry::orWhereFieldContainsWholeWord('your_custom_field', 'foo', 'bar')->get();

字段不包含整个单词

这就像 search:your_custom_field="not foo\W|bar\W"

Entry::whereFieldDoesNotContainWholeWord('your_custom_field', 'foo', 'bar')->get();

或者字段不包含整个单词的Where

Entry::orWhereFieldDoesNotContainWholeWord('your_custom_field', 'foo', 'bar')->get();

高级分类查询

这个库使用了Eloquent的关系功能。如果你需要执行比默认分类作用域更高级的分类查询,可以使用whereHasorWhereHas方法。

Entry::whereHas('categories', function ($query) {
    // category starts with A
    $query->where('cat_name', 'LIKE', 'A%');
})->get();

条目对象

每个条目对象都包含来自exp_channel_titles表的以下字符串属性。

$entry->entry_id
$entry->site_id
$entry->channel_id
$entry->author_id
$entry->forum_topic_id
$entry->ip_address
$entry->title
$entry->url_title
$entry->status
$entry->versioning_enabled
$entry->view_count_one
$entry->view_count_two
$entry->view_count_three
$entry->view_count_four
$entry->allow_comments
$entry->sticky
$entry->year
$entry->month
$entry->day
$entry->comment_total
$entry->page_uri

日期

条目有以下日期属性。这些将都是Carbon对象。 expiration_datecomment_expiration_daterecent_comment_date可以是null

$entry->entry_date
$entry->edit_date
$entry->expiration_date
$entry->comment_expiration_date
$entry->recent_comment_date

在toArray和toJson期间将日期序列化为ISO-8601格式。为此,将Carbon的默认格式设置为DateTime::ISO8601Y-m-d\TH:i:sO。如果你希望更改默认格式,应该在序列化之前调用\Carbon\Carbon::setToStringFormat($yourDateFormatString)。如果你希望在全球范围内将Carbon的此属性重置为原始默认值,应调用Carbon::resetToStringFormat()

频道对象

如果你需要有关条目的频道信息,有$entry->channel对象。频道对象包含来自exp_channels表的以下属性。

$entry->channel->channel_id
$entry->channel->site_id
$entry->channel->channel_name
$entry->channel->channel_title
$entry->channel->channel_url
$entry->channel->channel_description
$entry->channel->channel_lang
$entry->channel->total_entries
$entry->channel->total_comments
$entry->channel->last_entry_date
$entry->channel->last_comment_date
$entry->channel->cat_group
$entry->channel->status_group
$entry->channel->deft_status
$entry->channel->field_group
$entry->channel->search_excerpt
$entry->channel->deft_category
$entry->channel->deft_comments
$entry->channel->channel_require_membership
$entry->channel->channel_max_chars
$entry->channel->channel_html_formatting
$entry->channel->channel_allow_img_urls
$entry->channel->channel_auto_link_urls
$entry->channel->channel_notify
$entry->channel->channel_notify_emails
$entry->channel->comment_url
$entry->channel->comment_system_enabled
$entry->channel->comment_require_membership
$entry->channel->comment_use_captcha
$entry->channel->comment_moderate
$entry->channel->comment_max_chars
$entry->channel->comment_timelock
$entry->channel->comment_require_email
$entry->channel->comment_text_formatting
$entry->channel->comment_html_formatting
$entry->channel->comment_allow_img_urls
$entry->channel->comment_auto_link_urls
$entry->channel->comment_notify
$entry->channel->comment_notify_authors
$entry->channel->comment_notify_emails
$entry->channel->comment_expiration
$entry->channel->search_results_url
$entry->channel->show_button_cluster
$entry->channel->rss_url
$entry->channel->enable_versioning
$entry->channel->max_revisions
$entry->channel->default_entry_title
$entry->channel->url_title_prefix
$entry->channel->live_look_template

分类

每个Entry对象都有一个categories属性,它是一个Category对象的集合。使用withCategorieswithCategoryFields作用域来预加载此关系。

foreach ($entry->categories as $category) {
    echo '<li><a href="/blog/category/'.$category->cat_url_title.'">'.$category->cat_name.'</a></li>';
}
$category->cat_id
$category->site_id
$category->group_id
$category->parent_id
$category->cat_name
$category->cat_description
$category->cat_image
$category->cat_order
$category->your_custom_field

作者

每个Entry对象都有一个author属性,它是一个Member对象。使用withAuthorwithAuthorFields作用域来预加载此关系。

$entry->author->member_id
$entry->author->group_id
$entry->author->username
$entry->author->screen_name
$entry->author->password
$entry->author->salt
$entry->author->unique_id
$entry->author->crypt_key
$entry->author->authcode
$entry->author->email
$entry->author->url
$entry->author->location
$entry->author->occupation
$entry->author->interests
$entry->author->bday_d
$entry->author->bday_m
$entry->author->bday_y
$entry->author->aol_im
$entry->author->yahoo_im
$entry->author->msn_im
$entry->author->icq
$entry->author->bio
$entry->author->signature
$entry->author->avatar_filename
$entry->author->avatar_width
$entry->author->avatar_height
$entry->author->photo_filename
$entry->author->photo_width
$entry->author->photo_height
$entry->author->sig_img_filename
$entry->author->sig_img_width
$entry->author->sig_img_height
$entry->author->ignore_list
$entry->author->private_messages
$entry->author->accept_messages
$entry->author->last_view_bulletins
$entry->author->last_bulletin_date
$entry->author->ip_address
$entry->author->join_date
$entry->author->last_visit
$entry->author->last_activity
$entry->author->total_entries
$entry->author->total_comments
$entry->author->total_forum_topics
$entry->author->total_forum_posts
$entry->author->last_entry_date
$entry->author->last_comment_date
$entry->author->last_forum_post_date
$entry->author->last_email_date
$entry->author->in_authorlist
$entry->author->accept_admin_email
$entry->author->accept_user_email
$entry->author->notify_by_default
$entry->author->notify_of_pm
$entry->author->display_avatars
$entry->author->display_signatures
$entry->author->parse_smileys
$entry->author->smart_notifications
$entry->author->language
$entry->author->timezone
$entry->author->time_format
$entry->author->include_seconds
$entry->author->date_format
$entry->author->cp_theme
$entry->author->profile_theme
$entry->author->forum_theme
$entry->author->tracker
$entry->author->template_size
$entry->author->notepad
$entry->author->notepad_size
$entry->author->quick_links
$entry->author->quick_tabs
$entry->author->show_sidebar
$entry->author->pmember_id
$entry->author->rte_enabled
$entry->author->rte_toolset_id
$entry->author->your_custom_field

评论

每个Entry对象都有一个comments属性,它是一个Comment对象的集合。使用withComments作用域来预加载此关系。

$entry->comment->comment_id
$entry->comment->site_id
$entry->comment->entry_id
$entry->comment->channel_id
$entry->comment->author_id
$entry->comment->status
$entry->comment->name
$entry->comment->email
$entry->comment->url
$entry->comment->location
$entry->comment->ip_address
$entry->comment->comment_date
$entry->comment->edit_date
$entry->comment->comment
$entry->comment->author->member_id
$entry->comment->author->username
$entry->comment->author->screen_name

自定义字段

条目有它们的自定义字段作为属性,键为字段的短名称。大多数自定义字段属性仅是来自相应exp_channel_data field_id_X列的字符串数据。

$entry->your_field_name

对于以下字段类型,条目的自定义字段属性将是特殊对象,而不是来自exp_channel_data表的字符串数据。

矩阵 & 网格

矩阵 & 网格字段将是Row对象的Eloquent集合。每个Row对象都有字符串属性,键为从exp_matrix_dataexp_channel_grid_field_X表中的列短名称。自定义Row字段遵循与Entry自定义字段相同的逻辑。

$number_of_rows = $entry->your_matrix_field->count();

foreach ($entry->your_matrix_field as $row) {
    echo $row->your_text_column;
    foreach ($row->your_playa_column as $childEntry) {
        echo $childEntry->title;
    }
}

Playa & 关系

Playa & 关系字段将是相关Entry对象的Eloquent集合。这些Entry对象的行为与父Entry对象一样。

$number_of_rows = $entry->your_playa_field->count();

foreach ($entry->your_playa_field as $childEntry) {
    echo $childEntry->title;
}

资产

资产字段将是Asset对象的Eloquent集合。Asset对象有以下属性

foreach ($entry->your_assets_field as $file) {
    $file->url
    $file->server_path
    $file->file_id
    $file->folder_id
    $file->source_type
    $file->source_id
    $file->filedir_id
    $file->file_name
    $file->title
    $file->date
    $file->alt_text
    $file->caption
    $file->author
    $file->desc
    $file->location
    $file->keywords
    $file->date_modified
    $file->kind
    $file->width
    $file->height
    $file->size
    $file->search_keywords
}

文件

文件字段将是一个单一的File对象。File对象有以下属性

$entry->your_file_field->url
$entry->your_file_field->server_path
$entry->your_file_field->file_id
$entry->your_file_field->site_id
$entry->your_file_field->title
$entry->your_file_field->upload_location_id
$entry->your_file_field->rel_path
$entry->your_file_field->mime_type
$entry->your_file_field->file_name
$entry->your_file_field->file_size
$entry->your_file_field->description
$entry->your_file_field->credit
$entry->your_file_field->location
$entry->your_file_field->uploaded_by_member_id
$entry->your_file_field->upload_date
$entry->your_file_field->modified_by_member_id
$entry->your_file_field->modified_date
$entry->your_file_field->file_hw_original
echo '<img src="'.$entry->your_file_field->url.'" />';

日期

日期字段将是一个单一的Carbon对象。

echo $entry->your_date_field->format('Y-m-d H:i:s');

多选框、复选框、Fieldpack多选框、Fieldpack复选框 & Fieldpack列表

这些字段将是值数组

foreach ($entry->your_multiselect_field as $value) {
    echo $value;
}

扩展BasePlugin

抽象的rsanchez\Deep\Plugin\BasePlugin类作为ExpressionEngine模块和插件的基类提供。 parseEntries方法使用EntryCollection解析模板。

条目

<?php

use rsanchez\Deep\Plugin\BasePlugin;

class My_plugin extends BasePlugin
{
    public function entries()
    {
        return $this->parseEntries();
    }

    public function entries_that_start_with()
    {
        $letter = ee()->TMPL->fetch_param('letter');

        return $this->parseEntries(function ($query) use ($letter) {
            // do additional custom querying here
            $query->where('title', 'LIKE', $letter.'%');
        });
    }
}

现在你可以像频道:entries标签一样解析你的插件

{exp:my_plugin:entries channel="blog"}
  {title}
  {url_title_path="blog/view"}
{/exp:my_plugin:entries}

{exp:my_plugin:entries_that_start_with channel="blog" letter="A"}
  {title}
  {url_title_path="blog/view"}
{/exp:my_plugin:entries_that_start_with}

以下通道:entries 单个标签/条件表达式在 BasePlugin 类中未实现

  • gmt_entry_date
  • gmt_edit_date
  • member_search_path
  • relative_url
  • relative_date
  • trimmed_url
  • week_date

以下通道:entries 参数在 BasePlugin 类中未实现

  • display_by
  • dynamic_start
  • month_limit
  • paginate_type
  • relaxed_categories
  • show_current_week
  • track_views
  • week_sort
  • uncategorized_entries

parseEntries 方法具有以下默认参数

orderby="entry_date"
show_future_entries="no"
show_expired="no"
sort="desc"
status="open"
dynamic="yes"
limit="100"

您可以通过在插件/模块类中覆盖 getEntriesDefaultParameters 方法来更改这些参数

protected function getEntriesDefaultParameters()
{
    return array(
        'dynamic' => 'no',
        'status' => 'open|Featured',
    );
}

BasePlugin 类允许在 Matrix、Grid、Playa 和 Relationships 标签对中使用以下参数

  • limit
  • offset
  • orderby
  • sort
  • search:your_field
  • fixed_order
  • backspace
  • entry_id*
  • row_id**

*仅 Playa 和 Relationships **仅 Matrix 和 Grid

分类

BasePlugin 类还可以解析相当于 channel:categories 标签的内容

<?php

use rsanchez\Deep\Plugin\BasePlugin;

class My_plugin extends BasePlugin
{
    public function categories()
    {
        return $this->parseCategories();
    }

    public function offices()
    {
        $country = ee()->TMPL->fetch_param('country', 'us');

        ee()->TMPL->tagparams['style'] = 'linear';

        return $this->parseCategories(function ($query) use ($country) {
            return $query->channel('offices')->where('categories.cat_name', $country);
        });
    }
}

现在您可以将插件解析为 channel:categories 标签

{exp:my_plugin:categories channel="blog" style="nested"}
  <a href="{path='blog'}"{if active} class="active"{/if}>{category_name}</a>
{/exp:my_plugin:categories}

{exp:my_plugin:offices country="{segment_2}"}
{if no_results}{redirect="404"}{/if}
<h1><a href="{site_url}offices/{category_url_title}">{category_name}</a></h1>
{category_description}
{/exp:my_plugin:offices}

parseCategories 方法具有以下默认参数

show_empty="yes"
show_future_entries="no"
show_expired="no"
restrict_channel="yes"
style="nested"
id="nav_categories"
class="nav_categories"
orderby="categories.group_id|categories.parent_id|categories.cat_order"

您可以通过在插件/模块类中覆盖 getCategoriesDefaultParameters 方法来更改这些参数

protected function getCategoriesDefaultParameters()
{
    $params = parent::getCategoriesDefaultParameters();

    $params['style'] = 'linear';

    return $params;
}