0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

EC-CUBEでデータベースの値をページに呼び出す

Last updated at Posted at 2024-11-21

前提

データベースのテーブル
dtb_hoge

Entityファイル
app/Plugin/Hoge/Entity/Hoge.php

Repository作成

app/Plugin/Hoge/Repository/HogeRepository.php
<?php

namespace Plugin\Hoge\Repository;

use Eccube\Entity\Hoge; #Hogeテーブルと接続
use Symfony\Bridge\Doctrine\RegistryInterface; #Entityを利用する
use Eccube\Repository\AbstractRepository; #Repositoryを拡張
/**
 * HogeRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class HogeRepository extends AbstractRepository
{
    /**
     * HogeRepository constructor.
     *
     * @param RegistryInterface $registry
     */
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Hoge::class); #Entity/Hoge呼出
    }
    /**
     * 全検索
     *
     * @return Hoges|array
     */
    public function customFindAll() #関数名(任意)
    {
        $qb = $this->createQueryBuilder('h'); #任意だがhogeの頭文字
        $hoges = $qb
            ->getQuery() #作成したクエリを取得
            ->getResult(); #実行
        return $hoges;
    }
}

Controller作成

app/Plugin/Hoge/Controller/HogeController.php
<?php

namespace Plugin\Hoge\Controller;

use Eccube\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Plugin\Hoge\Repository\HogeRepository; #Repositoryを取得する為の宣言

class TestController extends AbstractController
{
    /**
     * @var HogeRepository
     */
    protected $hogeRepository;
    /**
     *
     *
     * HogeController constructor.
     *
     *  @param HogeRepository $hogetRepository
     *
     */
    public function __construct(HogeRepository $hogeRepository)
    {
      $this->hogeRepository = $hogeRepository;
    }
    /**
     *
     * @Route("/test", name="test") #URLを指定
     * @Template("test.twig") #ページ遷移先を指定
     *
     */
    public function index()
    {
        $hoges = $this->hogeRepository->customFindAll();
        return ['hoges'=>$hoges];
    }
}

Twig作成

app/Plugin/Hoge/Resource/test.twig
{% for hoges in hoges %}
<p>{{ hoge }}</p>
{% endfor %}

参考記事

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?