LoginSignup
7
7

More than 5 years have passed since last update.

cakephp3今課題に思っていること

Last updated at Posted at 2015-08-10

heroku の deploy

config/app.php
 'debug' => false,

にしてherokuにデプロイすると、Not Foundエラーで動かない。

日付表示のベストプラクティスが知りたい。

例)日付データがDBにはdate型で入っている

+------------+
| begin_date |
+------------+
| 2015-01-01 |
+------------+

これをfindすると、

begin_term(array)

    time 2015-01-01T00:00:00+0900
    timezone Asia/Tokyo
    fixedNowTime (false)

というようなオブジェクトで返ってくる。
これをそのままFormヘルパーに渡すと、勝手に年、月、日に分割したselectタグを生成してしまう。
が、自分は普通にdatepickerで選択させたいので、

<input type="text" id="datepicker" value="2015-01-01">

としたいのだが、やりかたがよくわからない
とりあえず、

src/Template/Posts/edit.ctp
use Cake\I18n\Time;
Time::setToStringFormat('YYYY-MM-dd');
echo $this->Form->text('begin_date', ['id' => 'begin_date']);

とすると時間のフォーマットが全部'YYYY-MM-dd'になるのでそれを使ってるが、あまりよくない。
(そのページ全部でこのフォーマットにする訳でもないので)

コントローラで、他のモデルの読み込み

src/Controller/UsersController.php
$this->loadModel('Offices');
$this->loadModel('Items');
$this->loadModel('Posts');

だらだらやってたら、こんな感じになっちゃったが、これは一行で書けないのか?

findの書き方の統一感

だらだら書いているとfindにもいろんな書き方が出てきて統一感がない。

src/Controller/OrdersController.php
    $query = $this->Orders->find();
    $query->where(['customer_id' => $customerId]);
    $this->set('orders', $this->paginate($query));

php:src/Controller/Customers Controller.php
$this->set('customer', $this->Customers->find()->where(['id' => $customerId])->toArray());

src/Controller/UsersController.php
    $user = $this->Users->get($id, [
        'contain' => ['Posts', 'Offices', 'Situations']
    ]);
    $this->set('user', $user);
src/Controller/UsersController.php
    $query = $this->Users->find();
    $query->where(['post_id' => $id, 'status' => 1]);
    $this->set('users', $query->first()->toArray());

コントローラのテスト

ログイン済みユーザー設定をいくつかあるテストのうち数個のテストにだけ共通に設定したいのだが。。
こんなやつ。

tests/TestCase/Controller/UsersControllerTest.php
    public function testPostAuthenticated(){
        // Set session data
        $this->session([
            'Auth' => [
                'User' => [
                    'id' => 1,
                    'name' => 'testname',
                    'username' => 'testing',
                    'role' => 'admin',
                    // other keys.
                ]
            ]
        ]);
        // 以下書略
    }
7
7
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
7
7