LoginSignup
0
1

More than 5 years have passed since last update.

Phalconのモデルでindex hintを使う

Posted at

はじめに

Phalconでもどんなframeworkでもですが、ORMを使うとIndex Hintを使う場合など、ちょっとカブいたSQLを書こうとしたらできなかったりしますよね。
だからって生PDOでSELECTしたらModelのfindを使った時とObjectが一致しないので、何かと不便です。
そういう時、Phalconなら、下記の方法で解決できますYO!

before

find使うと、使ってほしいindexが使われないじゃないか!どうしょ・・

$logs = Model\UserLog::find([
    "conditions" => "user_id = ?0 AND created BETWEEN ?1 AND ?2",
    "bind" => [$user_id, $from, $to],
    "order" => "created DESC",
    "limit" => 10
]);

after

そういう時は、(ほぼ)生PDOで抽出して、Modelに結果をあてこんじゃいましょう。

$model = new Model\UserLog();
$logs = new \Phalcon\Mvc\Model\Resultset\Simple(
    null,
    $model,
    $model->getReadConnection()->query(
        "SELECT * FROM user_log USE INDEX (idx_created) " .
        " WHERE user_id = ? AND created BETWEEN ? AND ? " .
        " ORDER BY created DESC LIMIT 10",
        [$user_id, $from, $to]
    )
);

まあ上記のようにサービスに書くくらいなら、Model側に書くべきですよね。。

小言

最近どのFWにも入ってるORMですが、オールドタイプなので何がいいのか未だに理解できないでいます。

0
1
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
0
1