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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图