cakephp3のTableオブジェクトをいろいろ見てみた2
クラス名と同じテーブル名を保有しないTableクラスを作成
ただの別のTableを使用するサンプルを作成
バリデーションとかができるかは別途調査する
app/App/Model/Table/SamplesTable.php
<?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\ORM\TableRegistry; class SamplesTable extends Table { protected $_table = null; public function initialize(array $config) { $this->table(null); } public function test1(){ $members = TableRegistry::get('Members'); return $members->find(); } }
一応、Tableをnullにする
Membersテーブルを読み込むサンプルを追加
適当に実装
<php $samples = TableRegistry::get('Samples'); $query = $samples->test1(); foreach ($query as $row) { var_dump($row); }
簡単な結果
class App\Model\Entity\Member#61 (9) { protected $_accessible => array(3) { 'name' => bool(true) 'age' => bool(true) 'tel' => bool(true) } protected $_properties => array(6) { 'id' => int(1) 'name' => string(24) "テスとユーザー1" 'age' =>
普通にこのレベルの使用なら可能。ベースとなるモデルを用意してその下で関連するTableをまとめて使用することでControllerのactionメソッドで複数のTableを記載するということは防ぐことが可能。
所感
マニュアルを読むだけでは???な部分が多いのでいろいろ手を動かして実際にどのように動くかを確認することで新たな発見がある。少しずつでも積み重ねていくことが大切のようです。