DB:exprについて

337 views
Skip to first unread message

yuzo urakami

unread,
Mar 16, 2014, 12:54:23 PM3/16/14
to fuelp...@googlegroups.com
お世話になります。
浦上と申します。

今開発でfuel1.6を使用していて、クエリビルダでand_whereにDB::exprを
使用して実行するとエラーになってしまいます。

$query = DB::select('i.id', 'i.title', 'i.flg_disp', array('date_format("i.input_date" , \'%Y/%m/%d\')', 'input_date'))
                ->from(array('informations', 'i'))
                ->where('i.deleted', '=', null)
                ->and_where('i.flg_disp', '<>', 1)
                ->and_where(DB::expr('IFNULL(i.from_date,\'000000000000\') <= \''' . date("YmdHi") . '\' and IFNULL(i.to_date , \'999999999999\') < \'''  . date("YmdHi") . '\'')));

上記を実行してプロファイルのand_whereの記述部分のsqlを見てみると
 AND 
 IFNULL(`i`.`from_date`,'00000000000000') <= '201403170000' 
 and 
 IFNULL(`i`.`to_date` , '99999999999999') >= '201403170000' null 
となっており最後にnullが表示されてしまいます。
色々と記述を試してみましたが、どれもうまくいかず困っております。
何か分かりましたらご教示ください。
以上よろしくお願いします。

Kenji Suzuki

unread,
Mar 16, 2014, 8:10:16 PM3/16/14
to fuelp...@googlegroups.com
Kenji です。


On Sun, 16 Mar 2014 09:54:23 -0700 (PDT)
yuzo urakami <yuzo.u...@gmail.com> wrote:

> お世話になります。
> 浦上と申します。
>
> 今開発でfuel1.6を使用していて、クエリビルダでand_whereにDB::exprを
> 使用して実行するとエラーになってしまいます。
>
> $query = DB::select('i.id', 'i.title', 'i.flg_disp',
> array('date_format("i.input_date" , \'%Y/%m/%d\')', 'input_date'))
> ->from(array('informations', 'i'))
> ->where('i.deleted', '=', null)
> ->and_where('i.flg_disp', '<>', 1)
> ->and_where(DB::expr('IFNULL(i.from_date,\'000000000000\')
> <= \''' . date("YmdHi") . '\' and IFNULL(i.to_date , \'999999999999\') <
> \''' . date("YmdHi") . '\'')));

私の環境(1.8/develop)だと、上記は PHP の文法エラーになりました。

$query = DB::select(
'i.id', 'i.title', 'i.flg_disp',
array('date_format("i.input_date" , \'%Y/%m/%d\')', 'input_date')
)->from(array('informations', 'i'))
->where('i.deleted', '=', null)
->and_where('i.flg_disp', '<>', 1)
->and_where(
DB::expr(
'IFNULL(i.from_date,\'000000000000\') <= \''
. date("YmdHi")
. '\' and IFNULL(i.to_date , \'999999999999\') < \''
. date("YmdHi")
. '\''
)
);

としたら、

SELECT `i`.`id`, `i`.`title`, `i`.`flg_disp`, date_format(`i`.`input_date` , '%Y/%m/%d') AS `input_date` FROM `informations` AS `i` WHERE `i`.`deleted` IS null AND `i`.`flg_disp` <> 1 AND IFNULL(i.from_date,'000000000000') <= '201403170101' and IFNULL(i.to_date , '999999999999') < '201403170101'

でした。


> 上記を実行してプロファイルのand_whereの記述部分のsqlを見てみると
> AND
> IFNULL(`i`.`from_date`,'00000000000000') <= '201403170000'
> and
> IFNULL(`i`.`to_date` , '99999999999999') >= '201403170000' null
> となっており最後にnullが表示されてしまいます。

最後に null が付くというはよくわからないですね。



// Kenji

yuzo urakami

unread,
Mar 17, 2014, 1:17:01 AM3/17/14
to fuelp...@googlegroups.com
Kenjiさん

ご返信ありがとうございます。
意図する動作になりました。

おそらく私の記述ミスによるものだと思います。
ご迷惑をお掛けしました。

2014年3月17日月曜日 9時10分16秒 UTC+9 Kenji Suzuki:
Reply all
Reply to author
Forward
0 new messages