【问题标题】:How can I check if a property exits in my object?如何检查我的对象中是否存在属性?
【发布时间】:2019-01-04 12:20:39
【问题描述】:

这是我的对象fields:

{#7139 ▼
  +"id": array:9 [▼
    "fieldName" => "id"
    "type" => "integer"
    "scale" => 0
    "length" => null
    "unique" => false
    "nullable" => false
    "precision" => 0
    "id" => true
    "columnName" => "id"
  ]
  +"name": array:8 [▼
    "fieldName" => "name"
    "type" => "string"
    "scale" => 0
    "length" => 255
    "unique" => false
    "nullable" => false
    "precision" => 0
    "columnName" => "name"
  ]
  +"unique_id": array:8 [▼
    "fieldName" => "unique_id"
    "type" => "string"
    "scale" => 0
    "length" => 10
    "unique" => true
    "nullable" => false
    "precision" => 0
    "columnName" => "unique_id"
  ]
  +"productgroup": array:20 [▼
    "fieldName" => "productgroup"
    "joinTable" => array:3 [▶]
    "targetEntity" => "App\Entity\Productgroup"
    "mappedBy" => null
    "inversedBy" => null
    "cascade" => []
    "orphanRemoval" => false
    "fetch" => 2
    "type" => 8
    "isOwningSide" => true
    "sourceEntity" => "App\Entity\Products"
    "isCascadeRemove" => false
    "isCascadePersist" => false
    "isCascadeRefresh" => false
    "isCascadeMerge" => false
    "isCascadeDetach" => false
    "joinTableColumns" => array:2 [▶]
    "isOnDeleteCascade" => true
    "relationToSourceKeyColumns" => array:1 [▶]
    "relationToTargetKeyColumns" => array:1 [▶]
  ]
}

我想获取包含relationToSourceKeyColumns的对象的名称

这是我的方法:

  foreach ($fields as $field) {
      $ManyToMany = property_exists($field['fieldName'], 'relationToSourceKeyColumns');
      if($ManyToMany == true){
        $result = $field['fieldName'];
      }
    }

结果是:

""

但我的预期结果是:

"productgroup"

【问题讨论】:

    标签: arrays symfony oop object properties


    【解决方案1】:

    函数property_exists 用于对象(https://secure.php.net/manual/en/function.property-exists.php)。在您的结构中搜索键在数组中,可能使用array_key_exists (https://secure.php.net/manual/en/function.array-key-exists.php)。

    【讨论】:

    • 啊,这是正确的解决方案:array_key_exists('relationToSourceKeyColumns', $field)
    猜你喜欢
    • 2021-10-08
    • 2020-06-20
    • 2019-03-27
    • 2016-01-17
    • 2018-01-01
    • 2020-05-08
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多