Skip to content

Commit

Permalink
Fix the relational depth (#133)
Browse files Browse the repository at this point in the history
* Fix the relational depth

- The root origin was overwritten
- Fix for issue #132

* Doc updates

* Fix test to actually be correct

* Test fixup

* Remove dead code

* A bit of abstraction for readability

* Fix doc settings

* Source is a string, not an array

* PHP-CS fixes

* PHP CS fixes
  • Loading branch information
Firesphere authored and marczhermo committed Nov 2, 2019
1 parent 15b2c59 commit 7c78fb8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
45 changes: 36 additions & 9 deletions src/Helpers/FieldResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function getNext(array $buildSources, $lookup): array

// @todo remove repetition
foreach ($buildSources as $source => $baseOptions) {
$next = $this->resolveRelation($source, $lookup, $next);
$next = $this->resolveRelation($source, $lookup, $next, $baseOptions);
}

$buildSources = $next;
Expand All @@ -121,20 +121,22 @@ protected function getNext(array $buildSources, $lookup): array
* @param string $source
* @param $lookup
* @param array $next
* @param array $options
* @return array
* @throws ReflectionException
* @throws Exception
*/
protected function resolveRelation($source, $lookup, array $next): array
protected function resolveRelation($source, $lookup, array $next, array &$options): array
{
$source = $this->getSourceName($source);

foreach (self::getHierarchy($source) as $dataClass) {
$schema = DataObject::getSchema();
$options = ['multi_valued' => false];
$options['multi_valued'] = false;

$class = $this->getRelationData($lookup, $schema, $dataClass, $options);

list($options, $next) = $this->getNextOption($next, $class, $options, $dataClass);
list($options, $next) = $this->handleRelationData($source, $next, $options, $class, $dataClass);
}

return $next;
Expand Down Expand Up @@ -271,25 +273,28 @@ protected function getRelationData($lookup, DataObjectSchema $schema, $className
}

/**
* Figure out the relational data for the given source etc.
*
* @param string $source
* @param array $next
* @param array|string $class
* @param array $options
* @param string $dataClass
* @param array|string|null $class
* @param string|null $dataClass
* @return array
*/
protected function getNextOption(array $next, $class, array $options, $dataClass): array
protected function handleRelationData($source, array $next, array &$options, $class, $dataClass)
{
if (is_string($class) && $class) {
if (!isset($options['origin'])) {
$options['origin'] = $dataClass;
$options['origin'] = $source;
}

// we add suffix here to prevent the relation to be overwritten by other instances
// all sources lookups must clean the source name before reading it via getSourceName()
$next[$class . '|xkcd|' . $dataClass] = $options;
}

return [$options, $next];
return array($options, $next);
}

/**
Expand Down Expand Up @@ -389,4 +394,26 @@ private function getFoundOriginData(

return $found;
}

/**
* @param array $next
* @param array|string $class
* @param array $options
* @param string $dataClass
* @return array
*/
protected function getNextOption(array $next, $class, array $options, $dataClass): array
{
if (is_string($class) && $class) {
if (!isset($options['origin'])) {
$options['origin'] = $dataClass;
}

// we add suffix here to prevent the relation to be overwritten by other instances
// all sources lookups must clean the source name before reading it via getSourceName()
$next[$class . '|xkcd|' . $dataClass] = $options;
}

return [$options, $next];
}
}
2 changes: 1 addition & 1 deletion src/Traits/CoreTraits/CoreAdminTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public function coreReload($core)
/**
* Check the status of a core
*
* @deprecated backward compatibility stub
* @param string $core
* @return StatusResult|null
* @deprecated backward compatibility stub
*/
public function coreIsActive($core)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/IndexTraits/BaseIndexTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ public function setSortFields($sortFields): self
/**
* Add a Fulltext Field
*
* @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore
* @param bool $includeSubclasses Compatibility mode, not actually used
* @throws ReflectionException
* @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore
*/
public function addFulltextFields($includeSubclasses = true)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/FieldResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public function testGetFieldIntrospection()
$factory->setIndex($index);

$expected = [
TestObject::class . '_TestObject_TestRelation_Title' =>
SiteTree::class . '_TestObject_TestRelation_Title' =>
[
'name' => TestObject::class . '_TestObject_TestRelation_Title',
'name' => SiteTree::class . '_TestObject_TestRelation_Title',
'field' => 'Title',
'fullfield' => 'TestObject_TestRelation_Title',
'origin' => TestObject::class,
'origin' => SiteTree::class,
'class' => TestRelationObject::class,
'type' => 'Varchar',
'multi_valued' => true,
Expand Down

0 comments on commit 7c78fb8

Please sign in to comment.