Cakephp 2.0 格式的内部连接输出



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

这是我的蛋糕加入

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

我怎样才能做到这一点??

非常感谢^^

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

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

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

不使用

连接。使用关系和可包含

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')));

最新更新