LoginSignup
5
2

More than 3 years have passed since last update.

EC-CUBE4 管理画面のCRUD 脱初心者三歩目 其ノ二 Repository編

Posted at

はじめに

前回のEntity編に続き、Repository編をやっていきます。

Entityはテーブルの設計図で、Repositoryを使ってテーブルからデータを取得する。
そんな感じだと思います。

Entity一つに対してRepositoryを一つ作りましょう。

Repositoryの作成

参考にする本体ソース:Eccube/Entity/CartRepository

CartRepositoryは一番シンプルな形で、基本的にはこの形でRepositoryを用意すればOKです。

それでは、前回作成したCustomize\Entity\Agency.phpを見てみましょう。
アノテーションで@ORM\Entity(repositoryClass="Customize\Repository\AgencyRepository")が設定しています。

CartRepositoryを参考にCustomize\Repository\AgencyRepository.phpを作成しましょう。

Customize\Repository\AgencyRepository.php
namespace Customize\Repository;

use Customize\Entity\Agency;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * AgencyRepository
 */
class AgencyRepository extends AbstractRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Agency::class);
    }
}

まずは、上記ファイルを設置するだけでOKです。

使い方

インジェクション

まずはインジェクションします。
「Symfony コンストラクタインジェクション」などで調べてみてください。

例としてEccube/Controller/Admin/Customer/を見てみます。

public function __construct(
    CustomerAddressRepository $customerAddressRepository
    ) {
    $this->customerAddressRepository = $customerAddressRepository;
}

CustomerAddressRepositoryを読み込んでいますね。(表現が正しいのか不明)

データの取得

find findAll findBy findOneByといった関数でデータを取得できます。
利用されてる部分を抜き出してみましょう。

// find
$Product = $this->productRepository->find($id);

// findAll
$Deliveries = $this->deliveryRepository->findAll();

// findBy
$ClassCategories1 = $this->classCategoryRepository->findBy(['ClassName' => $ClassName1], ['sort_no' => 'DESC']);

// findOneBy
$Delivery = $this->deliveryRepository->findOneBy([], ['sort_no' => 'DESC']);

findはidを指定してやり、一つのデータを取得します。
上記例では、IDを指定して商品データを取得しています。

findAllはすべてのデータを取得します。
配送方法のデータをすべて取得します。

findByはwhere条件とorderBy条件を指定してデータを取得します。
引数1にwhere条件の配列、引数2にOrderBy条件の配列を指定し、条件に合ったデータを配列で複数取得します。

findOneByfindByと同じでwhere条件とorderBy条件を指定してデータを取得します。
findByと違う部分は、最初のデータを一つだけ、配列データではなく、オブジェクトデータで取得します。

createQueryBuilderを使う

参考例:Eccube/Controller/Admin/Product/ProductClassController::findProduct()

findByはwhereとOrderBy条件だけでしたが、createQueryBuilder()を使い、自由度の高い条件でデータを取得する事ができます。

チェーンメソッドで->getQuery() ->getResult()と続けてデータを取得します。

getResult()は他にもgetSingleResult() getSingleScalarResult() getArrayResult()があります。

これらも配列だったりオブジェクトだったりが取得できます。
getSingleScalarResult()はcountした時、数値が取得できます。

Repositoryに関数を作成

一番シンプルな形でRepositoryを作成しましたが、Eccube/Repositoryのファイルを見て回ると、関数が結構作られています。

例えば、Eccube/Repository/ProductRepository::getQueryBuilderBySearchDataForAdmin()は、管理画面の商品一覧で検索された時の条件で絞り込む処理になっています。

Controllerに書くと長くなってしまうので、データ取得に関わる処理はRepositoryに書いてたりします。

AgencyRepositoryも、後々、検索処理などを実装していきます。

最後に

Repository編は以上で終わりです。

データの取得、createQueryBuilderを使うでは、EC-CUBEの本体でよく使われている処理で、紹介した使い方をマスターしておけば、十分ではないかと思います。

紹介した関数以外も用意されているっぽいのですが、今の所、僕は使った事はないです。

次回は、FormType編を書きます。お楽しみに!!

5
2
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
5
2