开发者

CLEARER UPDATE: self-referencing relationship is not working with datamapper in codeigniter

I read the documentation several times.

This is table structure:

categories:
id controller user_id approved enabled_comments

categories_related_categories
id category_id related_category_id

categories_zones
id category_id zone_id

This is my expectations:

I am trying to save multiple relations:

Expectations:

  1. New Category record is created (let’s call it subcategory but in reality no where is subcategory mentioned in database)
  2. The new category gets auto incremented to categories table and is given values for controller, user_id, enabled_comments fields passed from the query string of a http post request via the php form
  3. An existing category is searched (let’s call it parent category but in reality no where is parent category mentioned in the database)
  4. A new record is written to the categories_related_categories table where category_id refers to the primary key of the new category created and related_category_id refers to the id of the existing category we searched. Hence we create a many to many self-referencing relationship where a subcategory can have many categories and a parent category can have many subcategories
  5. A category can have many zones and a zone can have many categories. A user has the option to select one or many zones from a multiselect dropdown. If user selects two zones (each of them have a name using a name field like ‘main’ or ‘panel’), for example, then when the new category is written to the database, not only is a parent category established with it in the categories_related_categories table, but a many to many is established in the categories_zones table, where two records are created (if the user selects two options from multiselect), one record has the primary key of the new category with the one zone record selected from multiselect and the second record again has the primary key of the new cateogry with the other zone record selected from the multiselect.

So it’s the subcategory that has the relation with the parent and the zone(s):

This is the code:

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);

    }  

I get the following error with the above code (although the categories_zones table does get written to):

A PHP Error was encountered

Severity: Warning

Message: Illegal offse开发者_JAVA百科t type in isset or empty

Filename: libraries/Datamapper.php

Line Number: 4151  

I try this:

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

But I get this error:

An Error Was Encountered

Unable to relate category with relation. 

Then I try the example in this link: http://datamapper.wanwizard.eu/pages/save.html

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

THat will write to categories_related_categories table but not categories_zones table.

My category and zone model contains this:

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");
}

My temporary solution is the last comment (as of now): http://codeigniter.com/forums/viewthread/186054/


I know this is an old question, but I'll try helping out for anyone else with this question.

When you save multiple relationships, you can (and must on a self-referencing relationship) use a relationship_key that you defined in the model as the key in the array:

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

Take a look at the bottom of this page for the docs and example: http://datamapper.wanwizard.eu/pages/save.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜