unbindModel call in CakePhp. How does it work?

前端 未结 3 1538
星月不相逢
星月不相逢 2021-01-12 05:55

How does unbindModel happen in cake?

$this->User->unbindModel(array(\'hasAndBelongsToMany\' => array(\'Friend\')));

I wrote this i

相关标签:
3条回答
  • 2021-01-12 06:33

    From the manual:

    Removing or adding associations using bind- and unbindModel() only works for the next model operation unless the second parameter has been set to false. If the second parameter has been set to false, the bind remains in place for the remainder of the request.

    In other words, after you paginate() or find() or do anything else with the model, the unbinding will be reversed.

    0 讨论(0)
  • 2021-01-12 06:41

    Well, in my experience with unbinds, I can say Paginate always do 2 querys one for count the total and the second for the result array

    unbind destroy just once time the relation and yea you need extend this rule to destroy two or more times so you need to set TRUE I guess to persist this rule:

    $this->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')), true);
    
    0 讨论(0)
  • 2021-01-12 06:54

    Try This:

    $this->Leader->find('all');
    
    // Let's remove the hasMany...
    $this->Leader->unbindModel(
        array('hasMany' => array('Follower'))
    );
    
    // Now using a find function will return
    // Leaders, with no Followers
    $this->Leader->find('all');
    
    // NOTE: unbindModel only affects the very next
    // find function. An additional find call will use
    // the configured association information.
    
    // We've already used find('all') after unbindModel(),
    // so this will fetch Leaders with associated
    // Followers once again...
    $this->Leader->find('all');
    
    0 讨论(0)
提交回复
热议问题