11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC-CUBE4のrepository関数の使い方

Last updated at Posted at 2020-09-24

EC-CUBE4で利用できる、twig関数repositoryの使い方です。

通常、データを取得する際には、コントローラ側でデータを取得し、twigテンプレートへ渡す必要がありますが、repository関数を使えばtwigテンプレート内でデータ取得を行うことができます。

乱用すると可読性が悪くなるので注意が必要ですが、データがほしいけどphpのプログラムを触るのは敷居が高い、ような場合に使うと良いかと思います。

repository関数の文法は以下の通りです。

repository(エンティティクラス名).findBy({検索条件}, {ソート順}, 取得件数)

例えば、公開ステータスの新着商品を5件取得する場合は以下のようなサンプルになります。

{% set Products = repository('Eccube\\Entity\\Product').findBy({ Status: 1 }, { create_date: 'desc' }, 5) %}
{% for Product in Products %}
    {{ Product.name }}
{% endfor %}
11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?