Memo

メモ > 技術 > CMS: ECCube > カスタマイズ: 独自に追加したテーブルからデータを取得

■カスタマイズ: 独自に追加したテーブルからデータを取得
上記「ORMでテーブルを作成」の「テーブルの追加」で追加したテーブルから、 管理画面にデータを表示してみる Symfony2で利用されているDoctrineに入門する(後編) - OTOBANK Engineering Blog https://engineering.otobank.co.jp/entry/2017/05/09/190056 ※上記にある保存処理も、Repositoryにする方がいいか html\src\Customize\Repository\ContactRepository.php
<?php /* * This file is part of EC-CUBE * * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. * * http://www.ec-cube.co.jp/ * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Customize\Repository; use Customize\Entity\Contact; use Eccube\Repository\AbstractRepository; use Symfony\Bridge\Doctrine\RegistryInterface; /** * ContactRepository * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class ContactRepository extends AbstractRepository { public function __construct(RegistryInterface $registry) { parent::__construct($registry, Contact::class); } }
html\app\Customize\Controller\Admin\Content\ContactController.php
<?php /* * This file is part of EC-CUBE * * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. * * http://www.ec-cube.co.jp/ * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Customize\Controller\Admin\Content; use Eccube\Controller\AbstractController; use Customize\Repository\ContactRepository; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; class ContactController extends AbstractController { /** * @var ContactRepository */ protected $contactRepository; /** * ContactController constructor. * * @param ContactRepository $contactRepository */ public function __construct( ContactRepository $contactRepository ) { $this->contactRepository = $contactRepository; } /** * @Route("/%eccube_admin_route%/content/contact", name="admin_contact") * @Template("@admin/Content/contact.twig") */ public function index(Request $request) { $Contacts = $this->contactRepository->findAll(); return [ 'Contacts' => $Contacts, ]; } }
html\app\template\admin\Content\contact.twig
{% extends '@admin/default_frame.twig' %} {% set menus = ['content'] %} {% block title %}お問い合わせ{% endblock %} {% block sub_title %}コンテンツ管理{% endblock %} {% block main %} <div class="c-contentsArea__cols"> <div class="c-contentsArea__primaryCol"> <div class="c-primaryCol"> <div class="d-block mb-3"> <p>お問い合わせ一覧。</p> </div> <div class="card rounded border-0 mb-4"> <div class="card-body p-0"> <ul class="list-group list-group-flush mb-4 sortable-container"> <li class="list-group-item"> <div class="row justify-content-around"> <div class="col-1"><strong>ID</strong></div> <div class="col"><strong>Name</strong></div> <div class="col"><strong>Kana</strong></div> <div class="col"><strong>Email</strong></div> <div class="col"><strong>Created</strong></div> </div> </li> {% for Contact in Contacts %} <li class="list-group-item sortable-item" data-id="{{ Contact.id }}"> <div class="row justify-content-around"> <div class="col-1">{{ Contact.id }}</div> <div class="col">{{ Contact.name01 }} {{ Contact.name02 }}</div> <div class="col">{{ Contact.kana01 }} {{ Contact.kana02 }}</div> <div class="col">{{ Contact.email }}</div> <div class="col">{{ Contact.create_date|date('Y-m-d H:i:s') }}</div> </div> </li> {% endfor %} </ul> </div> </div> </div> </div> </div> {% endblock %}
管理画面にログインして以下にアクセスすると、お問い合わせ一覧が表示される /system/content/contact 表示されない場合、以下のコマンドでキャッシュを削除してから再度アクセスする
$ php bin/console cache:clear --no-warmup
データベースに「2020-08-17 06:27:51」と格納されていても、 上記のコードで一覧に「2020-08-17 15:27:51」と表示される つまり、表示の際に時差は調整されるみたい

Advertisement