Php Octobercms组件-关系上的限制结果
我有一个包含两个组件的插件。一个用于“帖子”,另一个用于“个人资料”。该配置文件属于一个用户,并且有许多“帖子” 但是,当我访问关系时,它会加载每个帖子。我只想加载5,然后分页或延迟加载,我如何才能做到这一点 Profile.php模型Php Octobercms组件-关系上的限制结果,php,octobercms,octobercms-plugins,Php,Octobercms,Octobercms Plugins,我有一个包含两个组件的插件。一个用于“帖子”,另一个用于“个人资料”。该配置文件属于一个用户,并且有许多“帖子” 但是,当我访问关系时,它会加载每个帖子。我只想加载5,然后分页或延迟加载,我如何才能做到这一点 Profile.php模型 public $hasMany = [ 'posts' => [ 'Redstone\Membership\Models\Post', 'table' => 'redstone_memb
public $hasMany = [
'posts' => [
'Redstone\Membership\Models\Post',
'table' => 'redstone_membership_profiles_posts',
]
];
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
]
];
public $belongsTo = [
'profile' => ['Redstone\Membership\Models\Profile',
'table' => 'redstone_membership_profiles_posts',
]
];
Post.php模型
public $hasMany = [
'posts' => [
'Redstone\Membership\Models\Post',
'table' => 'redstone_membership_profiles_posts',
]
];
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
]
];
public $belongsTo = [
'profile' => ['Redstone\Membership\Models\Profile',
'table' => 'redstone_membership_profiles_posts',
]
];
Profile.php组件
protected function loadProfile()
{
$id = $this->property('profileUsername');
$profile = new UserProfile
$profile = $profile->where(['slug' => $id]);
$profile = $profile->first();
return $profile;
}
profile/default.htm-组件视图
{% set profile = __SELF__.profile %}
{% set posts = __SELF__.profile.posts %}
{% for post in posts %}
<div class="card">
<div class="card-header">
<div class="ml-2">
<div class="h5 m-0">{{ profile.title }}</div>
</div>
{{ post.published_at|date('M d') }}
</div>
<div class="card-body text-left">
{{ post.content|raw }}
</div>
</div>
{% else %}
<h1>This user has not made any posts.</h1>
{% endfor %}
{%set profile=\u SELF\u.profile%}
{%set posts=\uuuuu SELF\uuuu.profile.posts%}
{posts%%中的post为%s}
{{profile.title}}
{{在|日期('md')发布}
{{post.content | raw}}
{%else%}
该用户尚未发表任何帖子。
{%endfor%}
您可以使用OctoberCMS或php函数进行操作,也可以通过twig构建函数
PHP使用slice:这是假设当您调用$profile->posts
时,您会得到一个posts集合。您还可以向Url添加一个查询,如example.com/profile?q=15
以将5更改为10 15等。我添加了一个if语句来检查以确保输入是数字且大于5
protected function loadProfile()
{
$id = $this->property('profileUsername');
$profile = UserProfile::where(['slug' => $id])->first();
if (is_numeric(Input::get('q')) == true && Input::get('q') > 5) {
$profile->posts = $profile->posts->slice(0, Input::get('q'));
} else {
$profile->posts = $profile->posts->slice(0, 5);
}
return $profile;
}
使用切片的细枝:这与PHP的方式非常相似,但在htm文件中完成
PHP-
小枝-
{% set query = __SELF__.getQuery %}
{% set profile = __SELF__.profile %}
{% set posts = __SELF__.profile.posts | slice(0, query) %}
{% for post in posts %}
<div class="card">
<div class="card-header">
<div class="ml-2">
<div class="h5 m-0">{{ profile.title }}</div>
</div>
{{ post.published_at|date('M d') }}
</div>
<div class="card-body text-left">
{{ post.content|raw }}
</div>
</div>
{% else %}
<h1>This user has not made any posts.</h1>
{% endfor %}
{%set query=\uuuuuu SELF\uuuuu.getQuery%}
{%set profile=\uuuuu SELF\uuuu.profile%}
{%set posts=uuu SELF_uuu.profile.posts |切片(0,查询)%}
{posts%%中的post为%s}
{{profile.title}}
{{在|日期('md')发布}
{{post.content | raw}}
{%else%}
该用户尚未发表任何帖子。
{%endfor%}
+1感谢您提供了多个示例!我甚至从未考虑过使用URL查询。我看到了paginate选项,但认为这是不可能的,因为配置文件的fetching->first()。是否可以仅使用->分页(15)对关系的结果进行分页?无论哪种方式,我都喜欢PHP使用slice的想法!不客气。你可以做的不仅仅是查询,还可以像加载更多按钮或搜索特定帖子等。10月份提供的分页服务相当不错,但确实需要一点学习曲线,我不确定你是否想要它。