English 中文(简体)
cakephp 删除阅读记录
原标题:cakephp delete threaded records
  • 时间:2012-05-11 06:24:50
  •  标签:
  • cakephp

I have implemented comments functionality in one of my sites using CakePHP and it s find( threaded ) feature.

When I delete a parent comment, I want all it s child comments (n-level children) to get deleted too, using CakePHP model s delete method.

情况如下:

id = 5名父母_id = 0;
id = 6名父母_id = 5;
id = 7名父母_id = 5;
id = 8名父母_id = 7;

And I do:

$this->Comment->id = 5;
$this->Comment->delete();

I want records with comment id = 5, 6, 7, 8 to get deleted.

Please advice.
Thanks

最佳回答

我先对以下法典进行了测试,但我要做的是:

<?php
class Comment extends AppModel {
    public function afterDelete() {
        $this->deleteChildren($this->id);
    }

    private function deleteChildren($parentId) {
        $children = $this->findByParentId($parentId);
        if (!empty($children)) {
            foreach ($children as $child) {
                $this->deleteChildren($child[ Comment ][ id ]);
                $this->delete($child[ Comment ][ id ]);
            }
        }
    }
}
?>
问题回答

Minor corrections in @greew s answer above and it worked like below for me:

public function afterDelete() {
     $this->__deleteChildren($this->id);
}

private function __deleteChildren($parentId) {
   $children = $this->findAllByParentId($parentId);
   if (!empty($children)) {
        foreach ($children as $child) {
           $this->delete($child[ Comment ][ id ]);
        }
    }
}

感谢许多贪 gr!





相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

Using DISTINCT in a CakePHP find function

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

Prevent controller from trying to autoload model

I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try to call ...

热门标签