Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
2.3 kB · 87 lines
PHP
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788<?php
/** * Load custom field data from storage. * * This query loads the data directly into the field objects and does not * return it to the caller. It can bulk load data for any list of fields, * even if they have different objects or object types. */final class PhabricatorCustomFieldStorageQuery extends Phobject {
private $fieldMap = array(); private $storageSources = array();
/** * @param array<PhabricatorCustomField> $fields */ public function addFields(array $fields) { assert_instances_of($fields, PhabricatorCustomField::class);
foreach ($fields as $field) { $this->addField($field); }
return $this; }
public function addField(PhabricatorCustomField $field) { $role_storage = PhabricatorCustomField::ROLE_STORAGE;
if (!$field->shouldEnableForRole($role_storage)) { return $this; }
$storage = $field->newStorageObject(); $source_key = $storage->getStorageSourceKey();
$this->fieldMap[$source_key][] = $field;
if (empty($this->storageSources[$source_key])) { $this->storageSources[$source_key] = $storage; }
return $this; }
public function execute() { foreach ($this->storageSources as $source_key => $storage) { $fields = idx($this->fieldMap, $source_key, array()); $this->loadFieldsFromStorage($storage, $fields); } }
private function loadFieldsFromStorage($storage, array $fields) { // Only try to load fields which have a persisted object. $loadable = array(); foreach ($fields as $key => $field) { $object = $field->getObject(); $phid = $object->getPHID(); if (!$phid) { continue; }
$loadable[$key] = $field; }
if ($loadable) { $data = $storage->loadStorageSourceData($loadable); } else { $data = array(); }
foreach ($fields as $key => $field) { if (array_key_exists($key, $data)) { $value = $data[$key]; $field->setValueFromStorage($value); $field->didSetValueFromStorage(); } else if (isset($loadable[$key])) { // NOTE: We set this only if the object exists. Otherwise, we allow // the field to retain any default value it may have. $field->setValueFromStorage(null); $field->didSetValueFromStorage(); } } }
}