让我们总结一下我想问的问题。
我有一个类别表和一个新闻表。
所以一个类别可以有许多新闻以及子类别。如果我删除一个类别,代码需要找到该类别的子类别以及与该类别相关的新闻,以及要删除的子类别。目前我的代码看起来像这样(目前运行良好):
关系:
public $_has_many = array(
'news' => array(
'model' => 'news',
'foreign_key' => 'cat_id'
)
);
要删除的代码:
/*
* @param $ids: The array of category id that we want to delete
*/
public function delete_items($ids){
if(!is_array($ids))
$ids = array($ids);
if(is_array($ids)){
$recursive = new System_Recursive();
/*
* list_items() method will simply return all records for the category table
*/
$source = $this->list_items(null);
/*
* Loop through the category ids
*/
foreach($ids as $id){
$result = $this->where('id', '=', $id)->find();
if($result->loaded()){
// If category found, then find all the news related to that category
$main_category_news = $result->news->find_all();
if($main_category_news){
// Loop through all the news and proccess the delete method
foreach($main_category_news as $main_news){
$main_news->delete();
}
}
/*
* The find_children() method returns all sub categories of the current category ($result)
*/
$recursive->find_children($source, $result->id, $arr_children, false);
if($arr_children){
// If any sub categories found, continue to loop :((, terrible
foreach($arr_children as $child){
$this->clear();
$child_result = $this->where('id', '=', $child)->find();
if($child_result->loaded()){
/*
* Again, find news related to this sub category and then loop through the news to do single delete
*/
$child_news = $child_result->news->find_all();
foreach($child_news as $c){
$c->delete();
}
}
/*
* After deleting news for sub category,
* I use clear to prevent error from loaded object
* Then find "again" the sub category to delete it
*/
$this->clear();
$child_delete = $this->where('id','=',$child)->find();
if($child_delete->loaded()){
$child_delete->delete();
}
}
}
/*
* And finally for the main direct category
*/
$this->clear();
$result = $this->where('id', '=', $id)->find();
$result->delete();
}
}
}
代码中有很多循环,假设我们删除了 50 个类别中的大约 5 个类别,每个类别都有 500 条相关新闻。我不知道,但我认为这需要一整天才能完成任务。
那么,有人可以提示我以正确的方式完成此代码:如何减少代码?如何减少循环?在这种情况下是否可以创建一个可重用的函数,例如,如果新闻有很多标签,我们会在这里做同样的事情,然后调用那个方法就可以了?
请帮我解决一下这个。如果你不回答,也请给我一个你不回答的理由,以便我提出更有意义的问题。
谢谢