• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to delete relationship with datamapper using delete()

#1
[eluser]johnmerlino[/eluser]
Hey all,

I looked at this documentation:

http://datamapper.wanwizard.eu/pages/delete.html

It says you can delete relationships with datamapper.

I have a categories table and categories_related_categories table.

categories table has primary key id and categories_related_categories table looks like this:

Code:
id category_id category_related_id
1  122         25

In the example above, there's a category with id of 122 and a category with id of 25. When I delete relationship I expect this record to disappear since there is no more relationship between the two records. But after using delete, the record remains in the categories_related_categories table. This is what I have:

Code:
public function update(){
            $vanity_url = new VanityUrl();
            $vanity_url->where('user_id',$this->current_user()->id)->get();
            
            $category = new Category();
            $category = $category->where('id',$this->uri->segment(4))->get();
             $category->controller = $this->input->post('controller');
            $category->enable_comments = $this->input->post('approved');
            
            $returned_controller = $this->input->post('parent_controller');
            
            $parent_category = new Category();
            $parent_category->where('controller',$returned_controller)->get();
            
            $zones = $this->input->post('zones');
            $zone = new Zone();
            $zone->where_in('name', $zones)->get();

            if($returned_controller == "none"){
                  $category->delete($category->related_category->get());
                if($category->save($zone->all)){
                    redirect("blogs/$vanity_url->url/categories");
                }
            }
            elseif($category->save( array($zone->all, 'related_category' => $parent_category))){
                $this->session->set_flashdata('flash_message', 'The category has been successfully updated.');
                redirect("blogs/$vanity_url->url/categories");
            }
            else {
                    $this->session->set_flashdata('flash_message', 'An error has occurred. Please try updating the category again.');
                    redirect("blogs/$vanity_url->url/categories/{$category->id}/edit");
            }
        }

When the user selects none from dropdown and returned_controller is then equal to none, this line executes:
Code:
$category->delete($category->related_category->get())
I know for a fact that $category->related_category->get() returns the right relationship because when I inspect it with var_dump, I see that it's the right category. So basically I am deleting the category associated with the current category. Yet when this function runs during update call, I look in database and the relationship is not deleted from the categories_related_categories table.

I also took a look at the "delete advanced relationships" section of that link I post. It appears to use a $related_ids variable, which is not defined anywhere and of course throws a php exception. But I don't believe I need that advanced section anyway because I am just trying to delete a category that is a parent of another category.

Thanks for response.

#2
[eluser]WanWizard[/eluser]
When you add check_last_query() after that delete, what does it show you?
How many related categories are there? Is none deleted?

According to the manual, delete either needs an object, or an array of objects. So try
Code:
// get all related categories
$category->related_category->get();

// and delete the relation
$category->delete($category->related_category->all);
In your case, it will use the return value of the get operation, which is the object with the first record found.

#3
[eluser]johnmerlino[/eluser]
Thanks for response. Your code was pretty similar to what I had and so it didn't do anything different. However, I did add the check_last_query function and it returned this:

Code:
DELETE FROM `categories_related_categories`
WHERE `related_category_id` = 122
AND `category_id` = 25

This is strange because the related_category has an id of 25 and the category has an id of 122. So this statement has it backwards.

This is the category model:

Code:
public $has_many = array(
            'related_category' => array(
                'class' => 'category',
                'other_field' => 'category',
               // 'reciprocal' => TRUE
            ),
            'category' => array(
                'other_field' => 'related_category',
            ),
            'post',
             'zone' => array(            
                        'class' => 'zone',    
                        'other_field' => 'category',    
                        'join_self_as' => 'category',    
                        'join_other_as' => 'zone',    
                        'join_table' => 'categories_zones',    
                )
         );

Not sure what I am doing wrong.

Now if I reverse it:

Code:
if($returned_controller == "none"){
                $category->related_category->get();
                $category->related_category->delete($category->all);  
               //    $category->delete($category->related_category->get());
                 if($category->save($zone->all)){
                    redirect("blogs/$vanity_url->url/categories");
                }
            }

it works fine. But while this works, it's weird because category is the child (e.g. 122) and related should be parent (25), since the uri segment grabs 122 and I want to find 122's parent, which is 25. Yet it seems like I have to invoke delete on the parent, not the child, as shown above. It's a little unintuitive.

And when I want to delete multiple relationships associated with a category, I have to do this:

Code:
if($category->related_category->delete($category) && $category->delete($zones->all) && $category->delete()){
                    $this->session->set_flashdata('flash_message', 'The category has been successfully deleted.');
                    redirect("blogs/{$vanity_url->url}/categories");
                }

when I would prefer to do this:

Code:
if($category->delete($category->related_category,$zones->all){}

Thanks for response.

#4
[eluser]lisahill[/eluser]
New Features:-

Added a new extension which allows you to use Datamapper for nested set tree management.
Added dutch language file.
Added a new extension which allows you to use Datamapper for nested set tree management.
It is now possible to define model relations, and update the model production cache, at runtime.
Updated the select() method to accept an array of column names next to a comma delimited string, like CodeIgniters Activerecord does.
The updated column can now be modified manually. The save method will save the update even if the updated timestamp is the only modified field.
The column name is now used as key of the error->all array, which allows you to reference the column name when iterating over the errors.
Added (experimental) support for reciprocal many-to-many relationships. See Advanced Relationships.
Added the option to define the name of the relationship table in an advanced relationship definition.
Added the option to run $object->{query}_related using a Datamapper object as $value. In case the object contains multiple values, the query will be transformed to a 'where_in' query using the id's in the objects resultset.
>Added the option to run $object->{query}_related using an array of id's. If the array contains multiple values, the query will be transformed to a 'where_in' query using the id's in the objects resultset.

Bug Fixes:-

Fixed problem with get_iterated when using PHP 5.3+ which doesn't do an implicit type conversion from array to object, causing an isset() to fail.
Fixed PHP fatal error when loading a model when using CI 2.0 packages or Modular CI, and a package or module is missing a models directory.
Fixed incorrect SQL count and SQL error on get_paged and get_paged_iterated when paging through a related table, linked to the parent using a relationship table, and including a where clause on a parent column.
Fixed race condition that could case a fatal error due to recursion when deleting ITFK's.

Other Changes:-

All language files have been converted to UTF-8.
Replaced hardcoded check for the MY_ prefix with the 'subclass_prefix' config value when autoloading classes.
When using include_join_fields, the id field of the relationship table is included as well.
Added some info about the post_model_init method to the documentation.
The manual now makes it clear that there are several reasons for a save to fail and gives some examples of what to check.

College Girls

#5
[eluser]WanWizard[/eluser]
@johnmerlino,

The issue here is that Datamapper fails to automagically guess the relationship, because it's a self relationship. You have the same model on both sides of the relation, so it picks the first one it can find. Which in your case is the wrong one, and that's why your relations are the wrong way around.

You will have to specify on which relationship you want to operate:
Code:
// save an explicit relation
$parent = new Category(1);
$child = new Category(2);

// related them
$parent->save_related_category($child);

// and delete it
$parent->delete_related_category($child);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.