Aug 21

Cakephp 学习日志 (二) 不指定

bkkkd , 07:54 , 开发应用 , 评论(0) , 引用(0) , 阅读(39763) , Via 本站原创 | |

通用的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();  
}  

bindModel()方法不单能创建一个关联,同样可以用来动态的修改一个关联。
下面是通常的用法:  

Generic bindModel() example  
$this->Model->bindModel(  
        array('associationName' => array(  
                'associatedModelClassName' => array(  
                    // normal association keys go here...  
                )  
            )  
        )  
    );  

注意:这些的前提是你的数据库表中的外键关联等已经正确设置。
[/quote]
内文分页: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Tags:
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]