dongyi9783 2015-03-13 21:21
浏览 55
已采纳

在CakePHP对象中查找值

Im trying to find a value inside a object, but I cant seem to get what isset is doing. Maybe Im using it wrong. I have two objects and what Im trying to do is delete the row which is not in the second object. Here is the object 1:

    Array ( 
    [idCategory1] => 1 
    [idCategory2] => 2 
    [idCategory4] => 4 
) 

And here is object 2:

 Array ( 
    [0] => Array ( 
        [CategoriesProvider] => Array ( 
            [id] => 28 
            [provider_id] => 2 
            [category_id] => 1 
            [created] => 2015-03-13 20:25:17 
            [modified] => 2015-03-13 20:25:17 
        ) 
    ) 
    [1] => Array ( 
        [CategoriesProvider] => Array ( 
            [id] => 29 
            [provider_id] => 2 
            [category_id] => 2 
            [created] => 2015-03-13 20:25:17 
            [modified] => 2015-03-13 20:25:17 
        ) 
    ) 
    [2] => Array ( 
        [CategoriesProvider] => Array ( 
            [id] => 30 
            [provider_id] => 2 
            [category_id] => 4 
            [created] => 2015-03-13 20:25:17 
            [modified] => 2015-03-13 20:25:17 
        ) 
    ) 
)

Here is my code to delete the row of object 2 which is not in object 1.

 foreach ($CategoriesOfProvider as $key ) {
    if(isset($CategoriesProvider['CategoriesProvider']->$key['CategoriesProvider']['category_id'])) {

    }  else {
        $this->User->Provider->CategoriesProvider->id = $key['CategoriesProvider']['id'];
        //$this->request->allowMethod('post', 'delete');
        if ($this->User->Provider->CategoriesProvider->delete()) {

        } else {
            $this->Session->setFlash(
                '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> No se pudo borrar la relacion producto - categoría.',
                'default',
                array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert')
            );
        }
    }
}

$CategoriesOfProvider is object 2 and $CategoriesProvider is object 1. So Im trying to use isset to find if the id in object 2 is in the value of the object 1. I think Im using isset wrong. Or is there another way? I thought of maybe run it against each of the values of object 1. Would that work better?

Thanks in advance for your help.

  • 写回答

1条回答 默认 最新

  • drjmrg8766 2015-03-13 22:10
    关注

    Ok I found out how to do it. I stopped using isset and instead use another foreach and went through all the values in object 1. If it wasnt found then I deleted the extra category. Here is the code.

    $this->User->Provider->CategoriesProvider->recursive = -1;
    $CategoriesOfProvider = $this->User->Provider->CategoriesProvider->find('all', array( 'conditions'=> array('CategoriesProvider.provider_id' => $provider_id ) ) );
    
    //echo print_r($CategoriesProvider);
    //return print_r($CategoriesOfProvider);
    
    foreach ($CategoriesOfProvider as $key ) {
        $found = false;
        //if(isset($CategoriesProvider['CategoriesProvider']->$key['CategoriesProvider']['category_id'])) {
        foreach ($CategoriesProvider as $key1 => $value) {
            if($key['CategoriesProvider']['category_id'] == $value) {
                $found = true;
            }  
        }
    
        if($found) {
    
        }  else {
            $this->User->Provider->CategoriesProvider->id = $key['CategoriesProvider']['id'];
            //$this->request->allowMethod('post', 'delete');
            if ($this->User->Provider->CategoriesProvider->delete()) {
    
            } else {
                $this->Session->setFlash(
                    '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> No se pudo borrar la relacion producto - categoría.',
                    'default',
                    array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert')
                );
            }
        }
    
    }
    

    I hope it helps someone :P

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?