###### tags: `yutons` # メモ ### CakePHP と MySQL の落とし穴 - mysql で datetime型を使っていると、date('Y-m-d H:i:s')で指定しないとmodelのバリデーションではじかれる --- - Error: SQLSTATE[HY000]: General error: 1525 Incorrect DATE value: '2023-06%' - ×例:SELECT id, name FROM users WHERE name = 'n%'; - 〇例:SELECT id, name FROM users WHERE name LIKE 'a%'; - cakePHPに置き換えると(Controller) ``` $Test = TableRegistry::getTableLocator()->get('Test'); $query = $Test->find() ->select(['id', 'name']) ->where(['name LIKE' => 'n%']) ->all(); ``` --- dateのフォーマットするとき エラー : [date() expects parameter 2 to be int, object given] - ×例:$today = date('Y-m', '2023-05-09T09:00:00+09:00'); - 〇例:$today = date('Y-m', strtotime('2023-05-09T09:00:00+09:00')); --- 複数検索(OR) ``` $this->find() ->where([ 'OR' => [ ['title LIKE' => '%検索ワード1%'], ['title LIKE' => '%検索ワード2%'] ] ]); ``` --- ``` $schedule_time = $this->find() ->select(['schedule_id', 'title', 'schedule_date', 'start_time', 'end_time', 'label_color_code']) ->where('schedule_date LIKE ' . $date . '%' ) ->all() ->toArray(); ``` Error: [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-05-31%' at line 1 これならエラー出ない ``` $schedule_time = $this->find() ->select(['title', 'start_time', 'end_time']) ->where(['schedule_date LIKE' => $date . '%']) ->all() ->toArray(); ``` --- js ``` document.querySelectorAll('.detail-schedule').forEach(el => { el.addEventListener('click', () => { }); }); ``` jquery ``` $(function(){ $('.detail-schedule').each(function (idx, el) { $(el).on('click', function(){ }) }) }); ``` function (idx, el) **バニラのほうindex抜けてるでしらんけど** - idxは要素の順番 - el は要素 - 例`<div id="test">テスト</div>` jQueryにおいて「$()」は関数です。 el.on(...)だと `Uncaught TypeError: el.on in not a function`エラーがでる --- jquery 奥深い https://hataworakuni.net/deference-between-attr-and-prop ---