正确创建 CakePHP 模型与 MongoDB 的关联
我正忙于使用 MongoDB 测试模型。我有两个模型:食谱和 标签。 <代码>
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,
)
);
}
食谱有很多标签,反之亦然,但我如何正确表示 这样它就可以通过 CakePHP 关联与 MongoDB 正确映射,因为模型 HABTM 等依赖于传统 SQL 关系数据?
此外,我如何确保关系得到管理 当我们删除菜谱或删除标签时正确吗?
我怀疑我必须创造一种行为来管理人际关系。 IE 中的 hasReference 和 isReferenced 很像 hasOne、hasAndBelongsToMany 等。
I'm busy testing Models with MongoDB. I have two Models: Recipe and
Tag.
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 has many tags and vica versa, but how do I correctly represent
this so that it maps correctly with MongoDB via CakePHP associations as the Model HABTM etc relay on traditional SQL relational data?
Futhermore, how do I ensure that the relationships are managed
correctly when we either delete a recipe or delete a tag?
I suspect I will have to create a behavior to manage the relationships. I.E. a hasReference and isReferenced much like hasOne, hasAndBelongsToMany, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前没有 MongoDB 数据源,它可以按照您在 CakePHP 中期望的方式处理模型关联。首先,NoSQL 数据库在概念上并不是为此而设计的,其次,CakePHP 1.x 模型/数据源架构使得实现如此多样化的数据库技术变得非常困难。
但是,您可以查看 Lithium 框架。他们已在其模型架构中实现了对 MongoDB 的完整“关系”支持。
Currently there is no MongoDB datasource, which handles model associations in a way you'd expect it in CakePHP. For one, NoSQL databases are conceptually not made for that, second, the CakePHP 1.x Model/Datasource architecture makes it very difficult to implement such diverse database technologies.
However, you might take a look at the Lithium framework. They have implemented full "relational" support for MongoDB into their model architecture.