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

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

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。