与MongoDB正确创建CakePHP模型关联


Correctly creating CakePHP Model Associations with MongoDB

我正忙着用MongoDB测试模型。我有两个模型:Recipe和标签。

class Recipe extends AppModel { 
    var $name = 'Recipe'; 
        var $mongoSchema = array( 
                'name' => array('type' => 'string'), 
                'description' => array('type' => 'string'), 
                'tags' => array('type' => 'array'), //the tags "tag" value 
                'created' => array('type' => 'datetime'), 
                'modified' => array('type' => 'datetime'), 
        ); 
    var $hasAndBelongsToMany = array( 
        'Tag' => 
            array( 
                'className'              => 'Tag', 
                'joinTable'              => 'recipe', 
                'foreignKey'             => '_id', 
                'associationForeignKey'  => 'tags', 
                'unique'                 => true, 
            ) 
    ); 
} 
class Tag extends AppModel { 
    var $name = 'Tag'; 
        var $mongoSchema = array( 
                'tag' => array('type' => 'string'), 
                'description' => array('type' => 'string'), 
                'created' => array('type' => 'datetime'), 
                'modified' => array('type' => 'datetime'), 
        ); 
    var $hasAndBelongsToMany = array( 
        'Recipe' => 
            array( 
                'className'              => 'Recipe', 
                'joinTable'              => 'recipe', 
                'foreignKey'             => 'tags', 
                'associationForeignKey'  => '_id', 
                'unique'                 => true, 
            ) 
    ); 
} 

Recipe有许多标签,反之亦然,但我如何正确地表示这使得它通过CakePHP关联与MongoDB正确映射为模型HABTM等中继对传统的SQL关系数据?

此外,我如何确保管理关系当我们删除食谱或删除标签时正确吗?

我想我将不得不创建一个行为来管理这些关系。例如,hasReference和isreferreferi很像hasOne, hasAndBelongsToMany等

目前没有MongoDB数据源,它以您在CakePHP中期望的方式处理模型关联。首先,NoSQL数据库在概念上并不是为此而设计的,其次,CakePHP 1。x Model/Datasource架构使得实现这种多样化的数据库技术非常困难。

但是,您可以看一下Lithium框架。他们已经在他们的模型架构中实现了对MongoDB的完整的"关系"支持。