0

Datamapperを使用していて、結果セットの関連テーブル(1:1の関係)のすべての列を結合したいと考えています。したがって、私のコードは次のようになります。

$p = new Project();
$arrAll = $p->where("id <", 100)->where_related('outboundform', 'reference_type', 'project')->include_related('outboundform')->get()->all_to_array();
print_r($arrAll);

クエリは機能しますが、テーブル'outboundform'の列が結果に表示されず、完全に無視されます!! チェックしたところ、生成されたSQLは次のようになりました。

SELECT `project` . * , `outboundform`.`id` AS outboundform_id, `outboundform`.`reference_type` AS outboundform_reference_type, `outboundform`.`reference_id` AS outboundform_reference_id, `outboundform`.`created` AS outboundform_created, `outboundform`.`updated` AS outboundform_updated, `outboundform`.`v1` AS outboundform_v1, `outboundform`.`v2` AS outboundform_v2, `outboundform`.`v3` AS outboundform_v3, `outboundform`.`v4` AS outboundform_v4, `outboundform`.`v5` AS outboundform_v5 FROM (`project`) LEFT OUTER JOIN `outboundform` outboundform ON `project`.`id` = `outboundform`.`reference_id` WHERE `project`.`id` <100 AND `outboundform`.`reference_type` = 'project' LIMIT 0 , 30

これはOKで、実行すると正しい結果が得られます。ここでのDatamappersの問題は何ですか?全量の列が返されないのはなぜですか?

4

1 に答える 1

0

関連するoutboundform行のプロパティは、リレーションの名前がプレフィックスとして付けられた結果オブジェクトでアクセスできる必要があります。そのため、にidなるoutboundform_idreference_typeなるoutboundform_reference_typeなどです。

問題は、all_to_array()それらを結果の配列バージョンに転送しないことです。それらを含めたい場合は、明示的にリストする必要があります。

$arrAll = $p->/* snip */->all_to_array(array('id', 'name', 'outboundform_id', 'outboundform_reference_type')); // Project fields and outboundform fields too

任意のモデルのインスタンスがある場合、そのモデルのプロパティによって (データベース テーブルから) フィールドのリストを取得し、それらを使用して、含まれるリレーションのプレフィックス名を作成することにより$instance->fields、呼び出しのリストを作成できます。all_to_array

[]または、フィールドにアクセスする方法だけが必要な場合は、次のArrayAccessようなインターフェイスを実装できます。

class DataMapper2 extends DataMapper implements ArrayAccess
{ 
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            throw new ErrorException('model instances doesn\'t support pushing new fields');
        } else {
            $this->{$offset} = $value;
        }
    }
    public function offsetExists($offset)
    {
        return isset($this->{$offset});
    }
    public function offsetUnset($offset)
    {
        unset($this->{$offset});
    }
    public function offsetGet($offset)
    {
        return isset($this->{$offset}) ? $this->{$offset} : null;
    }
}
于 2012-11-21T17:20:37.790 に答える