如何检查对象中是否存在属性?

时间:2019-01-04 12:20:39

标签: arrays symfony oop object properties

这是我的对象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"

1 个答案:

答案 0 :(得分:3)

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