422 123
发新话题
打印

CakePHP中文手册- - the rapid development php framework

使用 bindModel() 和 unbindModel() 实时地改变关联关系
有的时候可能你会需要实时地,动态的改变model的关联关系,比如在一个异常情况下。特别是LazyLoad的问题,有的时候我们并需要一个完整的model,所以我们可以使用 bindModel() 和 unbindModel()来绑定或者解除绑定关联对象。

代码说话,Start:

leader.php and follower.php  
<?php  
  
class Leader extends AppModel  
{  
    var $name = 'Leader';  
  
    var $hasMany = array(  
        'Follower' => array(  
            'className' => 'Follower',  
            'order'     => 'Follower.rank'  
        )  
    );  
}  
  
?>  
  
<?php  
  
class Follower extends AppModel  
{  
    var $name = 'Follower';  
}  
  
?>  
         
leader.php and follower.php
<?php

class Leader extends AppModel
{
    var $name = 'Leader';

    var $hasMany = array(
        'Follower' => array(
            'className' => 'Follower',
            'order'     => 'Follower.rank'
        )
    );
}

?>

<?php

class Follower extends AppModel
{
    var $name = 'Follower';
}

?>
                 
上述两个Model,在Leader Model中,有一个hasMany关联,定义了 "Leader hasMany Followers" 这样的关系。下面我们演示如何在Controller中动态地解除这种关联绑定。

leaders_controller.php (partial)  
function someAction()  
{  
    //This fetches Leaders, and their associated Followers  
    $this->Leader->findAll();  
  
    //Let's remove the hasMany...  
    $this->Leader->unbindModel(array('hasMany' => array('Follower')));
     
    //Now a using a find function will return Leaders, with no Followers
    $this->Leader->findAll();

    //NOTE: unbindModel only affects the very next find function.
    //注意:unbindModel方法只作用一次,第二次find方法调用时则仍然是关联关系有效的
    //An additional find call will use the configured association information.

    //We've already used findAll() after unbindModel(), so this will fetch  
    //Leaders with associated Followers once again...  
    $this->Leader->findAll();  
}  
         
leaders_controller.php (partial)
function someAction()
{
    //This fetches Leaders, and their associated Followers
    $this->Leader->findAll();

    //Let's remove the hasMany...
    $this->Leader->unbindModel(array('hasMany' => array('Follower')));
   
    //Now a using a find function will return Leaders, with no Followers
    $this->Leader->findAll();

    //NOTE: unbindModel only affects the very next find function.
    //注意:unbindModel方法只作用一次,第二次find方法调用时则仍然是关联关系有效的
    //An additional find call will use the configured association information.

    //We've already used findAll() after unbindModel(), so this will fetch
    //Leaders with associated Followers once again...
    $this->Leader->findAll();
}
                 
对于其他各种关联的unbindModel()的用法是类似的,你只需要更改名字和类型就可以了,下面介绍一些基础的Usage:

TOP

通用的unbindModel()

$this->Model->unbindModel(array('associationType' => array('associatedModelClassName')));

掌握了如何动态的解除绑定之后,让我们看看如何动态的绑定关联关系。

leaders_controller.php (partial)  
funciton anotherAction()  
{  
    //There is no Leader hasMany Principles in the leader.php model file, so  
    //a find here, only fetches Leaders.  
    $this->Leader->findAll();  
  
    //Let's use bindModel() to add a new association to the Principle model:  
    $this->Leader->bindModel(  
        array('hasMany' => array(
                'Principle' => array(
                    'className' => 'Principle'
                )
            )
        )
    );

    //Now that we're associated correctly, we can use a single find function  
    //to fetch Leaders with their associated principles:  
    $this->Leader->findAll();  
}  
         
leaders_controller.php (partial)
funciton anotherAction()
{
    //There is no Leader hasMany Principles in the leader.php model file, so
    //a find here, only fetches Leaders.
    $this->Leader->findAll();

    //Let's use bindModel() to add a new association to the Principle model:
    $this->Leader->bindModel(
        array('hasMany' => array(
                'Principle' => array(
                    'className' => 'Principle'
                )
            )
        )
    );

    //Now that we're associated correctly, we can use a single find function
    //to fetch Leaders with their associated principles:
    $this->Leader->findAll();
}
                 
bindModel()方法不单能创建一个关联,同样可以用来动态的修改一个关联。

下面是通常的用法:  
  
Generic bindModel() example  
$this->Model->bindModel(  
        array('associationName' => array(  
                'associatedModelClassName' => array(  
                    // normal association keys go here...  
                )  
            )  
        )  
    );  
         
下面是通常的用法:

Generic bindModel() example
$this->Model->bindModel(
        array('associationName' => array(
                'associatedModelClassName' => array(
                    // normal association keys go here...
                )
            )
        )
    );
                 
注意:这些的前提是你的数据库表中的外键关联等已经正确设置。

<未完,请关注本站更新>

TOP

 422 123
发新话题