douxian5076 2015-01-10 15:01
浏览 48
已采纳

CakePHP递归不使用loadModel和this-> set

Recursion does not seem to be working when I use $this->loadModel, I want to say I remember something about having to explicitly set the belongsTo and hasMany in this situation, but that seems anti-cakephp-auto-magical, I have yet to find any answers stating how to use

$this->loadModel 

and

$this->...->find('all', array('recursive' => 2));  

any ideas on why this isnt working is greatly appreciated.

$this->loadModel('DeployablesJoin');
$deployablesJoins = $this->DeployablesJoin->find('all', array('recursive' => 2));

$this->loadModel('DeployablesComplete');
$deployablesCompletes = $this->DeployablesComplete->find('all', array('recursive' => 2, 'conditions' => array('successful' => array('0', '-1'))));

$this->set('deployablesCompletes', $deployablesCompletes);
$this->set('deployablesJoins', $deployablesJoins);

Edit: Full Code Below

My DeployablesJoin Model

class DeployablesJoin extends NodeCncAppModel {

    public $actsAs = array('Containable');

    public $belongsTo = array(
        'Deployable' => array(
            'className' => 'Deployable',
            'foreignKey' => 'deployable_id',
            'conditions' => '',
            'fields' => 'title,filename,created',
            'order' => ''
        ),
        'ServerClass' => array(
            'className' => 'ServerClass',
            'foreignKey' => 'server_class_id',
            'conditions' => '',
            'fields' => 'name',
            'order' => ''
        )
    );
}

Relevant part of the controller

    $this->loadModel('DeployablesJoin');
    $deployablesJoins = $this->DeployablesJoin->find('all');

    print_r($deployablesJoins); die();

Results from print_r/die

Array ( 
    [0] => Array ( 
        [DeployablesJoin] => Array ( 
            [id] => 1 
            [deployable_id] => 5 
            [server_class_id] => 1 
        ) 
    ) 
)

What I have found that does work is the following (relevant part controller)

$this->loadModel('DeployablesJoin');
$this->DeployablesJoin->belongsTo = array(
    'Deployable' => array(
        'className' => 'Deployable',
        'foreignKey' => 'deployable_id',
        'conditions' => '',
        'fields' => 'title,filename,created',
        'order' => ''
    ),
    'ServerClass' => array(
        'className' => 'ServerClass',
        'foreignKey' => 'server_class_id',
        'conditions' => '',
        'fields' => 'name',
        'order' => ''
    )
);
$deployablesJoins = $this->DeployablesJoin->find('all');

print_r($deployablesJoins); die();

Results from print_r/die

Array ( 
    [0] => Array ( 
        [DeployablesJoin] => Array ( 
            [id] => 1 
            [deployable_id] => 5 
            [server_class_id] => 1 
        ) 
        [Deployable] => Array ( 
            [title] => asdf 
            [filename] => 5_agent.zip 
            [created] => 2015-01-09 21:31:25 
        ) 
       [ServerClass] => Array ( 
            [name] => Crawler 
       ) 
 )

When I do a print_r/die on $this->DeployablesJoin, I get the following

AppModel Object
(
    [useDbConfig] => default
    [useTable] => deployables_joins
    [id] => 
    ...
    [table] => deployables_joins
    [primaryKey] => id
    ...
    [name] => DeployablesJoin
    [alias] => DeployablesJoin
    [tableToModel] => Array
        (
            [deployables_joins] => DeployablesJoin
        )

    [cacheQueries] => 
    [belongsTo] => Array
        (
        )
  • 写回答

2条回答 默认 最新

  • douwu7168 2015-01-10 17:00
    关注

    There doesn't appear to be any reason your 'recursive' would not be working other than the associations set up incorrectly. I would suggest verifying those.

    That being said, using recursive 2 is considered bad practice. Instead, set recursive to -1, and use CakePHP's amazing Containable Behavior to retrieve any additional information.

    Another "best practice" change would be to put your finds into model methods instead of in the Controllers. So, in your case, it would be something like this:

    // in the controller
    $this->loadModel('DeployablesJoin');
    $deployablesJoins =  $this->DeployableJoin->getAll();
    
    //in the DeployablesJoin model
    public function getAll() {
        $this->recursive = 2; // should change this to containable instead though
        return $this->find('all');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器