Path: blob/master/src/infrastructure/customfield/query/PhabricatorCustomFieldStorageQuery.php
13419 views
<?php12/**3* Load custom field data from storage.4*5* This query loads the data directly into the field objects and does not6* return it to the caller. It can bulk load data for any list of fields,7* even if they have different objects or object types.8*/9final class PhabricatorCustomFieldStorageQuery extends Phobject {1011private $fieldMap = array();12private $storageSources = array();1314public function addFields(array $fields) {15assert_instances_of($fields, 'PhabricatorCustomField');1617foreach ($fields as $field) {18$this->addField($field);19}2021return $this;22}2324public function addField(PhabricatorCustomField $field) {25$role_storage = PhabricatorCustomField::ROLE_STORAGE;2627if (!$field->shouldEnableForRole($role_storage)) {28return $this;29}3031$storage = $field->newStorageObject();32$source_key = $storage->getStorageSourceKey();3334$this->fieldMap[$source_key][] = $field;3536if (empty($this->storageSources[$source_key])) {37$this->storageSources[$source_key] = $storage;38}3940return $this;41}4243public function execute() {44foreach ($this->storageSources as $source_key => $storage) {45$fields = idx($this->fieldMap, $source_key, array());46$this->loadFieldsFromStorage($storage, $fields);47}48}4950private function loadFieldsFromStorage($storage, array $fields) {51// Only try to load fields which have a persisted object.52$loadable = array();53foreach ($fields as $key => $field) {54$object = $field->getObject();55$phid = $object->getPHID();56if (!$phid) {57continue;58}5960$loadable[$key] = $field;61}6263if ($loadable) {64$data = $storage->loadStorageSourceData($loadable);65} else {66$data = array();67}6869foreach ($fields as $key => $field) {70if (array_key_exists($key, $data)) {71$value = $data[$key];72$field->setValueFromStorage($value);73$field->didSetValueFromStorage();74} else if (isset($loadable[$key])) {75// NOTE: We set this only if the object exists. Otherwise, we allow76// the field to retain any default value it may have.77$field->setValueFromStorage(null);78$field->didSetValueFromStorage();79}80}81}8283}848586