Php 处理pivot数据操作的正确方法(via有许多“到”)
在一个基本的博客应用程序中,我有一个文章模块,允许我选择相关的类别。 有3张桌子;文章,文章,分类,分类。扩展ORM的两个模型,Php 处理pivot数据操作的正确方法(via有许多“到”),php,orm,kohana,kohana-3,kohana-3.2,Php,Orm,Kohana,Kohana 3,Kohana 3.2,在一个基本的博客应用程序中,我有一个文章模块,允许我选择相关的类别。 有3张桌子;文章,文章,分类,分类。扩展ORM的两个模型,Model\u Article和Model\u Category具有许多相应的“直通”关系。在“编辑文章”表单提交过程中,正确的方式(或更好的方式)是什么: 以数组格式检索所有可用的类别ID,以填充表单::select() 从透视表中以选定类别的数组格式检索分配的类别ID 成功提交表单后更新透视表,其中已存在一些透视数据。最好在添加/删除相关数据之前检查现有数据(而不
Model\u Article
和Model\u Category
具有许多相应的“直通”关系。在“编辑文章”表单提交过程中,正确的方式(或更好的方式)是什么:
- 以数组格式检索所有可用的
,以填充类别ID
表单::select()
- 从透视表中以选定类别的数组格式检索分配的
类别ID
- 成功提交表单后更新透视表,其中已存在一些透视数据。最好在添加/删除相关数据之前检查现有数据(而不是删除所有现有数据,然后添加所有提交的
)类别ID
$categories_orm = ORM::factory('category')->find_all();
foreach ($categories_orm as $category)
{
$categories[$categories_orm->id] = $categories_orm->name;
}
$article = ORM::factory('article', $post['article_id']);
// do stuff (validate, update the article etc.)
// remove all relationships
$article->remove('categories');
// add new relationships
$article->add('categories', $post['categories']);
不确定我的要求是否只能通过数据库准备语句/查询生成器来实现。如果是这样,我是否建议将自己的方法(使用准备好的语句/查询生成器)添加到我的模型文章extend ORM
我目前使用的方法是:
获取所有类别ID
$categories_orm = ORM::factory('article_category')->find_all();
foreach($categories_orm as $category_orm) {
$categories[$category_orm->id] = $category_orm->name;
}
$categories_current = $article->article_categories->find_all();
foreach($categories_current as $category){
$categories_selected = $category->id;
}
$categories_orm = $article->article_categories->find_all();
$categories_existing = array();
foreach($categories_orm as $category){
$categories_existing[] = $category->id;
}
$categories_selected = $this->request->post('category');
$categories_to_remove = array_diff($categories_existing,$categories_selected);
$categories_to_add = array_diff($categories_selected,$categories_existing);
if(!empty($categories_to_remove)) {
$article->remove('article_categories', $categories_to_remove);
}
if(!empty($categories_to_add)) {
$article->add('article_categories', $categories_to_add);
}
获取所选类别ID
$categories_orm = ORM::factory('article_category')->find_all();
foreach($categories_orm as $category_orm) {
$categories[$category_orm->id] = $category_orm->name;
}
$categories_current = $article->article_categories->find_all();
foreach($categories_current as $category){
$categories_selected = $category->id;
}
$categories_orm = $article->article_categories->find_all();
$categories_existing = array();
foreach($categories_orm as $category){
$categories_existing[] = $category->id;
}
$categories_selected = $this->request->post('category');
$categories_to_remove = array_diff($categories_existing,$categories_selected);
$categories_to_add = array_diff($categories_selected,$categories_existing);
if(!empty($categories_to_remove)) {
$article->remove('article_categories', $categories_to_remove);
}
if(!empty($categories_to_add)) {
$article->add('article_categories', $categories_to_add);
}
更新类别ID
$categories_orm = ORM::factory('article_category')->find_all();
foreach($categories_orm as $category_orm) {
$categories[$category_orm->id] = $category_orm->name;
}
$categories_current = $article->article_categories->find_all();
foreach($categories_current as $category){
$categories_selected = $category->id;
}
$categories_orm = $article->article_categories->find_all();
$categories_existing = array();
foreach($categories_orm as $category){
$categories_existing[] = $category->id;
}
$categories_selected = $this->request->post('category');
$categories_to_remove = array_diff($categories_existing,$categories_selected);
$categories_to_add = array_diff($categories_selected,$categories_existing);
if(!empty($categories_to_remove)) {
$article->remove('article_categories', $categories_to_remove);
}
if(!empty($categories_to_add)) {
$article->add('article_categories', $categories_to_add);
}
您可能应该将关系表重命名为
articles\u categories
(两个复数形式)以遵循Kohana约定,categories
是关系的更好别名
类别模型应用于检索所有类别ID。然后按照与示例相同的方式循环遍历它以填充数组
获取所有类别。
$categories_orm = ORM::factory('category')->find_all();
foreach ($categories_orm as $category)
{
$categories[$categories_orm->id] = $categories_orm->name;
}
$article = ORM::factory('article', $post['article_id']);
// do stuff (validate, update the article etc.)
// remove all relationships
$article->remove('categories');
// add new relationships
$article->add('categories', $post['categories']);
确保select元素在数组中提供post值,方法如下所示:
name="categories[]"
更新关系。
$categories_orm = ORM::factory('category')->find_all();
foreach ($categories_orm as $category)
{
$categories[$categories_orm->id] = $categories_orm->name;
}
$article = ORM::factory('article', $post['article_id']);
// do stuff (validate, update the article etc.)
// remove all relationships
$article->remove('categories');
// add new relationships
$article->add('categories', $post['categories']);
我可能会使用隐藏的表单字段进行检查,以首先验证类别关系是否需要更新。我可以想象,一篇文章的分类几乎从未改变过