はじめに
OneToOneの関係のテーブルにデータを取得(Select)する。
「OneToOne データ追加(Insert)」の続き。
データ取得(Select)
Controllerよりアクセス
ControllerからRepository経由でアクセス
app/Customize/Controller/SamplePageController.php
/**
* @Route("/sample/{id}", name="sample_index")
* @Template("Sample/index.twig")
*/
public function index($id)
{
$product = $this->productRepository->find($id);
dump($product);
return [
'shop_name' => $this->baseInfo->getShopName(),
'product_name' => $product->getName(),
'maker_name' => $product->getMakerName(),
'size_a' => $product->getProductSize()->getSizeA(),
];
}
ブラウザよりアクセス
/sample/1 でdtb_productとdtb_product_sizeのデータを取得できた。
Product {#3838 ▼
-_calc: false
-stockFinds: []
-stocks: []
-stockUnlimiteds: []
-price01: []
-price02: []
-price01IncTaxs: []
-price02IncTaxs: []
-codes: []
-classCategories1: []
-classCategories2: []
-className1: null
-className2: null
-id: 2
-name: "チェリーアイスサンド"
-note: null
-description_list: null
-description_detail: """
チェリーアイスサンドは北海道産のチェリーのアイスをサクサクのクッキーでサンドしたスイーツです。立方体なので大量に持ち運ぶときも便利です。\n
いまだけ、頑丈な箱つきです。
"""
-search_word: null
-free_area: null
-create_date: DateTime @1538129692 {#3841 ▶}
-update_date: DateTime @1538129692 {#3840 ▶}
-ProductCategories: PersistentCollection {#3877 ▶}
-ProductClasses: PersistentCollection {#3875 ▶}
-ProductImage: PersistentCollection {#3931 ▶}
-ProductTag: PersistentCollection {#3989 ▶}
-CustomerFavoriteProducts: PersistentCollection {#4040 ▶}
-Creator: null
-Status: ProductStatus {#4080 ▶}
-AnnotationReader: AnnotationReader {#261 ▶}
+maker_name: "メーカー2"
-ProductSize: ProductSize {#4066 ▼
-id: 2
-product_id: 2
-size_a: 20
-size_b: 200
-create_date: DateTime @1545902935 {#4069 ▶}
-update_date: DateTime @1545902935 {#4068 ▶}
-Product: Product {#3838}
-AnnotationReader: AnnotationReader {#261 ▶}
}
}
まとめ
親をfind()して子オブジェクトを参照。