Help us understand the problem. What is going on with this article?

FuelPHPでORMのモデル関係においてconditionsを使用してみる

More than 1 year has passed since last update.

はじめに

ORMを使用した場合でモデル関係で条件を付与してデータを取得したかったのでそのメモになります。以下がわかりやすかったです。感謝!
参考:FuelPHPのORMでconditionsを持ったRelationの書き方。

環境
Amazon Linux
FuelPHP 1.8

詳細


テーブル構成

例えばテーブル構成が以下のような場合

Aテーブル
APKey field1
1 dog
2 cat
3 bard
Bテーブル
BPKey APKey field1
1 1 pochi
2 1 hana
3 2 momo
4 2 nyanko
5 2 tama
6 3 momo

条件を付与するにはconditionsを追加すればOKです。

class Model_Atable extends \Orm\Model {
        :
    protected static $_has_many = array(
            'bTable'  => array(
                'model_to'          => 'bTable'
            ,   'key_from'          => 'APKey'
            ,   'key_to'            => 'APKey'
            ,   'cascade_save'      => false
            ,   'cascade_delete'    => false
            ,   'conditions'        => array(
                   'where'     => array(array('field1','=','momo'))
                ,  'order_by'  => array('BPKey'   => 'asc')
                )
            )
    );

結果

とりあえずタスクで実行するやつ書きます。

<?php
namespace Fuel\Tasks;
class Ormtest {
    public function run($arg) {
        $query = Model_Atable::query();
        $query->related("bTable");
        $rows = $query->get();
        print_r($rows);
    }
}

結果

$ oil refine Ormtest
Array
(
    [0] => Array
        (
            [APKey] => 1
            [field1] => dog
            [bTable] => Array
            (
            )
        )
    [1] => Array
        (
            [APKey] => 2
            [field1] => cat
            [bTable] => Array
            (
                [0] => Array
                    (
                         [BPKey] => 3
                         [APKey] => 2
                         [field1] => momo
                    )
            )
        )
    [2] => Array
        (
            [APKey] => 3
            [field1] => bard
            [bTable] => Array
            (
                [0] => Array
                    (
                         [BPKey] => 6
                         [APKey] => 3
                         [field1] => momo
                    )
            )
        )
)

あとがき

条件式の値をconst定義から取ろうとしたらクエリーの解析でカラムフィールドと間違えられて少しはまりました。
実値にすれば問題なくできました。この部分が少しはまったくらいでした。

その他

参考
FuelPHPのORMでconditionsを持ったRelationの書き方。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away