3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP2.x 独自SQLの場合でDBからのデータをまとめたいときのTips

3
Last updated at Posted at 2015-07-09

CakePHP使い始めて間もないのですが、わからないことばかり。

とくに独自SQLを書いたときに取得しようとするとなぜだか配列がいくつかに分かれてしまう。とくにASで別名したときなどに不便さを感じました。
CakePHP調べてたら仕様だとのことです。

これは、バーチャルフィールドというものを使って取得する必要があるようです。
http://book.cakephp.org/2.0/ja/models/virtual-fields.html

例
$this->ModelName1->virtualFields['originalName1'] = 0;

こんな感じで定義して、SQL文の中で
AS ModelName1__originalName1

さらに
FROM Table_name AS ModelName1
としてテーブル名とモデル名を合わせておく必要があります。
こうすると取得したデータがすべて、array["ModelName1"]にまとまって取得できます。

こちらを参考にさせていただきました!
http://i723.sakura.ne.jp/wp/archives/179

以上!

Qiita初投稿!

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?