0

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

这是我的 cakephp 加入

$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
                )
            )
        )
)

我怎样才能做到这一点?

非常感谢^^

4

2 回答 2

2

您可以在 Cakephp 中使用模型关联而不是使用连接

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

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

于 2012-11-27T13:47:16.960 回答
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')));
于 2012-11-27T13:40:49.693 回答