Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

HelloWorld 3回目

モデルと

Helloコントローラ
<?php
namespace App\Controller;

use App\Controller\AppController;

class HelloController extends AppController {
    public $autoRender = true;
    //public $name = "Hello";


    public function index()
    {
        /// Hogesモデルを利用
        $this->loadModel('Hoges');

//        echo "hello world!!";
    //src/Template/Layout/default.ctp レイアウトの使用
        $this->viewBuilder()->autoLayout(true);
        $this->ViewBuilder()->layout('Hello'); //Hello.ctpつかう。

// セットした値はテンプレート側で$poi等で見える。
        $this->set('poi',100);
        $this->set('poi2',200);
        $this->set('poi3',"hoge");

        // Hogesテーブルのすべてをとってくる
        $query = $this->Hoges->find(); // { "id": 1, "setting": 112121 }配列
        $this->set('hoges',$query);

        
    }

    public function other()
    {
        echo "hello other world!!";
    }
}
?>
Helloテンプレ

デフォルトレイアウトが適用されている。

    <h1>midashi</h1>
    <p> hello hello </p>
    <?= $poi ?>
    <?= $poi2 ?>
    <?= $poi3 ?>
    <?= $hoges ?>


    <?php foreach ($hoges as $obj): ?>
    <?= $obj ?>
    <?php endforeach; ?>

<!-- ヘルパーを使ってテーブルで表示 -->
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>SETTING</th>
    </tr>
  </thead>
  <?php
  $arr = $hoges->toArray();// 配列に取り出す
  for ($i = 0 ; $i < count($arr); $i++) {
    echo $this->Html->tableCells(
      $arr[$i]->toArray(),
      ['style' => 'background-color:#f0f0f0'],
      ['style' => 'font-weight:bold'],
      true);
  }
    ?>
  </table>


  <?= $this->element('fooelement', ['va'=>$hoges]) ?>

エレメント
<!-- $vaを必要とする? -->
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>SETTING</th>
    </tr>
  </thead>
  <?php
  $arr = $va->toArray();// 配列に取り出す
  for ($i = 0 ; $i < count($arr); $i++) {
    echo $this->Html->tableCells(
      $arr[$i]->toArray(),
      ['style' => 'background-color:#f0f0f0'],
      ['style' => 'font-weight:bold'],
      true);
  }
    ?>
  </table>
*** Hogesテーブル(bakeで作った)
>|php|
<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Hoges Model
 *
 * @method \App\Model\Entity\Hoge get($primaryKey, $options = [])
 * @method \App\Model\Entity\Hoge newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Hoge[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Hoge|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Hoge patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Hoge[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Hoge findOrCreate($search, callable $callback = null, $options = [])
 */
class HogesTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('hoges');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        $validator
            ->integer('setting')
            ->allowEmpty('setting');

        return $validator;
    }
}