Cakephp 2.0格式内连接输出

时间:2012-11-27 13:29:11

标签: php cakephp

我需要格式化以下蛋糕php join,而不必在我的控制器中运行一个丑陋的PHP脚本。

这是我的cakephp join

$this->Type->find('all' , array(
        'conditions' => array( 'Type.id' => '2' ),
        'joins' => array(
          array(
            'table' => 'subtypes',
            'alias' => 'Subtype',
            'conditions' => 'Subtype.tid = Type.id'
         )
      )
 ));

这是我得到的输出

Array
(
    [0] => Array
        (
            [Type] => Array
                (
                    [id] => 4
                    [name] => Clay
                    [handle] => CL
                    [description] => 
                )

            [Subtype] => Array
                (
                    [id] => 11
                    [tid] => 4
                    [name] => Water Based
                )

        )

    [1] => Array
        (
            [Type] => Array
                (
                    [id] => 4
                    [name] => Clay
                    [handle] => CL
                    [description] => 
                )

            [Subtype] => Array
                (
                    [id] => 12
                    [tid] => 4
                    [name] => Oil Based
                )

        )
)

这是我想要的输出

Array
(
    [0] => Array
        (
            [Type] => Array
                (
                    [id] => 4
                    [name] => Clay
                    [handle] => CL
                    [description] => 
                )

            [Subtype] => Array(
                [0] => Array(
                    [id] => 11
                    [tid] => 4
                    [name] => Water Based
                )
                [1] =>Array
                (
                    [id] => 12
                    [tid] => 4
                    [name] => Oil Based
                )
            )
        )
)

我怎样才能实现这个目标?

非常感谢^^

2 个答案:

答案 0 :(得分:2)

您可以在Cakephp中使用Model Association而不是使用连接

供您参考: - http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

如果您需要任何帮助,请告诉我们!

答案 1 :(得分:1)

不要使用连接。 使用关系和可包含的

public $hasMany = array('Subtype'); // your foreign key should be "type_id" not "tid"

$this->Type->find('all', array(
    'conditions' => array('Type.id' => '2'),
    'contain'=>array('Subtype')));