从表字段octobercms排序数据

时间:2018-07-30 07:25:38

标签: sorting octobercms

我想按十进制以num为单位的列顺序显示数据,

default.htm

{% set post = __SELF__.post %}

<div class="content">{{ post.content_html|raw }}</div>

{% if post.featured_images.count %}
    <div class="featured-images text-center">
        {% for image in post.featured_images %}
            <p>
                <img
                    data-src="{{ image.filename }}"
                    src="{{ image.path }}"
                    alt="{{ image.description }}"
                    style="max-width: 100%" />
            </p>
        {% endfor %}
    </div>
{% endif %}

<p class="info">
    Posted
    {% if post.categories.count %} in
        {% for category in post.categories %}
            <a href="{{ category.url }}">{{ category.name }}</a>{% if not loop.last %}, {% endif %}
        {% endfor %}
    {% endif %}
    on {{ post.published_at|date('M d, Y') }}
</p>
<p class="info">

    {% for blocks in post.blocks %}  // here the output of this loop must show in order which are given in column in same table which is relation_sort_order
    <b> {{ blocks.title}}</b>

    {{ blocks.content_html|raw }}

    {% endfor %}
</p>

我如何在relation_sort_order字段中以数字显示块

1 个答案:

答案 0 :(得分:0)

假设post变量是一个自定义Model对象,而blocks是该对象上的关系,则执行要求的最佳方法是更新模型中的关系定义定义您的post对象的类。

您未包含该代码,但可能看起来像这样:

public $hasMany = [
    'blocks' => 'Acme\Blog\Models\Block'
]

您可以对其进行更新以默认包含排序顺序,请参见此section of the OctoberCMS documentation

但是简而言之,您可以将上面的内容更改为此:

public $hasMany = [
    'blocks' => [
        'Acme\Blog\Models\Block', 
        'order' => 'relation_sort_order asc'
    ]
];

然后您每次访问blocks类中的Post关系时,它都会自动按照该顺序对其进行排序。

相关问题