0

我多次阅读文档。

这是表结构:

categories:
id controller user_id approved enabled_comments

categories_related_categories
id category_id related_category_id

categories_zones
id category_id zone_id

这是我的期望:

我正在尝试保存多个关系:

期望:

  1. 创建了新的类别记录(我们称之为子类别,但实际上数据库中没有提到子类别)
  2. 新类别自动递增到类别表,并通过 php 表单从 http post 请求的查询字符串传递给控制器​​、user_id、enabled_comments 字段的值
  3. 搜索现有类别(我们称其为父类别,但实际上数据库中没有提及父类别)
  4. 一条新记录被写入到 categories_related_categories 表中,其中 category_id 指的是新创建的类别的主键,related_category_id 指的是我们搜索的现有类别的 id。因此,我们创建了一个多对多的自引用关系,其中一个子类别可以有很多类别,而父类别可以有很多子类别
  5. 一个类别可以有多个区域,一个区域可以有多个类别。用户可以从多选下拉列表中选择一个或多个区域。例如,如果用户选择了两个区域(每个区域都有一个名称,使用像“main”或“panel”这样的名称字段),那么当将新类别写入数据库时​​,不仅会在其中建立一个父类别categories_related_categories 表,但是在 categories_zones 表中建立了多对多,其中创建了两条记录(如果用户从多选中选择了两个选项),一条记录具有新类别的主键,其中一条区域记录是从多选中选择的并且第二条记录再次具有新类别的主键,并且从多选中选择了另一个区域记录。

所以它是与父级和区域有关系的子类别:

这是代码:

public function create(){
        $vanity_url = new VanityUrl();
        $vanity_url->where('url',$this->uri->segment(2))->get();
        $blogger = new User();
        $blogger->where('id',$vanity_url->user_id)->get();

        /* self-referencing many to many */            
        $subcategory = new Category; //subcategory - we create a new one
        $subcategory->controller = $this->input->post('controller');
        $subcategory->enable_comments = $this->input->post('approved');
        $subcategory->user_id = $this->current_user()->id;

        $parent_category = new Category(); //parent category - we find via a search in the table
        $parent_category->where('controller',$this->input->post('parent_controller'))->get();
    //    $subcategory->save($parent_category);

        /* many to many - category has many zones :: zone has many categories */
        $zone = new Zone();
        $zones = $this->input->post('zones');

        foreach($zones as $z){
            switch($z){
                case 'main':
                    $zone->where('name',$z);
                    break;
                case 'panel':
                    $zone->where('name',$z);
                    break;
            }
        }
        $zone->get();

        $subcategory->save($parent_category,$zone);

    }  

上面的代码出现以下错误(尽管确实写入了 categories_zones 表):

A PHP Error was encountered

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: libraries/Datamapper.php

Line Number: 4151  

我试试这个:

$subcategory->save_relation($parent_category,$zone);  

但我得到这个错误:

An Error Was Encountered

Unable to relate category with relation. 

然后我尝试这个链接中的例子:http: //datamapper.wanwizard.eu/pages/save.html

$subcategory->save(array($parent_category,$zone));

这将写入 categories_related_categories 表,但不会写入 categories_zones 表。

我的类别和区域模型包含以下内容:

class Category extends DataMapper {
    var $has_one = array("user");

public $has_many = array(

        'related_category' => array(
            'class' => 'category',
            'other_field' => 'category',
           // 'reciprocal' => TRUE
        ),
        'category' => array(
            'other_field' => 'related_category',
        ),
        'post',
        'zone'
    );

}

class Zone extends DataMapper {
    var $has_many = array("category");
}

我的临时解决方案是最后一条评论(截至目前): http ://codeigniter.com/forums/viewthread/186054/

4

1 回答 1

0

我知道这是一个老问题,但我会尝试帮助其他人解决这个问题。

当您保存多个关系时,您可以(并且必须在自引用关系上)使用relationship_key您在模型中定义的 a 作为数组中的键:

$subcategory->save(
    array(
        'category' => $parent_category,
        'zone' => $zone
    )
);

查看此页面底部的文档和示例: http ://datamapper.wanwizard.eu/pages/save.html

于 2011-09-30T19:37:31.547 回答