Skip to content

Commit

Permalink
fix: if xml value is set, change value in class metadata (doctrine-ex…
Browse files Browse the repository at this point in the history
  • Loading branch information
gfuentesboost authored and phansys committed Sep 3, 2023
1 parent 52e6411 commit 05e2457
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ a release.
## [Unreleased]
### Changed
- Add conflict with "doctrine/orm" >=2.16 (temporary change until code is fixed)
### Fixed
- References: fixed condition in `XML` Driver that did not allow to retrieve from the entity definition the `mappedBy` and `inversedBy` fields.

### Fixed
- Fix bug collecting metadata for inherited mapped classes
Expand Down
2 changes: 2 additions & 0 deletions schemas/orm/doctrine-extensions-mapping-2-2.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<xs:element name="loggable" type="gedmo:loggable"/>
<xs:element name="soft-deleteable" type="gedmo:soft-deleteable"/>
<xs:element name="uploadable" type="gedmo:uploadable"/>
<xs:element name="reference" type="gedmo:reference"/>
<!-- field -->
<xs:element name="slug" type="gedmo:slug"/>
<xs:element name="translatable" type="gedmo:translatable"/>
Expand Down Expand Up @@ -65,6 +66,7 @@
<xs:complexType name="reference">
<xs:attribute name="type" type="gedmo:reference-type" use="required" />
<xs:attribute name="field" type="xs:string" use="required" />
<xs:attribute name="reference" type="xs:string" use="required" />
<xs:attribute name="identifier" type="xs:string" use="required" />
<xs:attribute name="class" type="xs:string" use="required" />
<xs:attribute name="mappedBy" type="xs:string" use="optional" />
Expand Down
4 changes: 2 additions & 2 deletions src/References/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public function readExtendedMetadata($meta, array &$config)
'identifier' => $identifier,
];

if (!$this->_isAttributeSet($element, 'mappedBy')) {
if ($this->_isAttributeSet($element, 'mappedBy')) {
$config[$reference][$field]['mappedBy'] = $this->_getAttribute($element, 'mappedBy');
}
if (!$this->_isAttributeSet($element, 'inversedBy')) {
if ($this->_isAttributeSet($element, 'inversedBy')) {
$config[$reference][$field]['inversedBy'] = $this->_getAttribute($element, 'inversedBy');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getChildrenQueryBuilder($node = null, $direct = false, $sortByFi
$qb->field($config['path'])->equals(new Regex($regex));
}

$qb->sort($sortByField ?? $config['path'], 'asc' === $direction ? 'asc' : 'desc');
$qb->sort($sortByField ?? $config['path'], 'asc' === strtolower($direction) ? 'asc' : 'desc');

return $qb;
}
Expand All @@ -150,7 +150,7 @@ public function getChildrenQuery($node = null, $direct = false, $sortByField = n
return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode)->getQuery();
}

public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
return $this->getChildrenQuery($node, $direct, $sortByField, $direction, $includeNode)->execute();
}
Expand Down
8 changes: 6 additions & 2 deletions src/Tree/Entity/Repository/AbstractTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ abstract public function getNodesHierarchyQuery($node = null, $direct = false, a
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return QueryBuilder QueryBuilder object
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);

Expand All @@ -225,10 +227,12 @@ abstract public function getChildrenQueryBuilder($node = null, $direct = false,
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return Query Query object
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);

Expand Down
12 changes: 9 additions & 3 deletions src/Tree/Entity/Repository/ClosureTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ public function getPath($node)
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return QueryBuilder QueryBuilder object
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function childrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
Expand Down Expand Up @@ -186,10 +188,12 @@ public function childrenQueryBuilder($node = null, $direct = false, $sortByField
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return Query Query object
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function childrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
Expand All @@ -200,10 +204,12 @@ public function childrenQuery($node = null, $direct = false, $sortByField = null
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return array|null List of children or null on failure
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function children($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Tree/Entity/Repository/MaterializedPathRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
*/
public function getTreeQueryBuilder($rootNode = null)
{
return $this->getChildrenQueryBuilder($rootNode, false, null, 'asc', true);
return $this->getChildrenQueryBuilder($rootNode, false, null, 'ASC', true);
}

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ public function getPath($node)
return $this->getPathQuery($node)->getResult();
}

public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
$meta = $this->getClassMetadata();
$config = $this->listener->getConfiguration($this->_em, $meta->getName());
Expand Down Expand Up @@ -206,18 +206,18 @@ public function getChildrenQueryBuilder($node = null, $direct = false, $sortByFi
}

$orderByField = null === $sortByField ? $alias.'.'.$config['path'] : $alias.'.'.$sortByField;
$orderByDir = 'asc' === $direction ? 'asc' : 'desc';
$orderByDir = 'asc' === strtolower($direction) ? 'asc' : 'desc';
$qb->orderBy($orderByField, $orderByDir);

return $qb;
}

public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode)->getQuery();
}

public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
return $this->getChildrenQuery($node, $direct, $sortByField, $direction, $includeNode)->execute();
}
Expand Down
27 changes: 15 additions & 12 deletions src/Tree/Entity/Repository/NestedTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,12 @@ public function getPathAsString(object $node, array $options = []): string
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return QueryBuilder QueryBuilder object
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function childrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
Expand Down Expand Up @@ -382,10 +384,12 @@ public function childrenQueryBuilder($node = null, $direct = false, $sortByField
* @param object|null $node if null, all tree nodes will be taken
* @param bool $direct true to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return Query Query object
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function childrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
Expand All @@ -396,10 +400,12 @@ public function childrenQuery($node = null, $direct = false, $sortByField = null
* @param object|null $node The object to fetch children for; if null, all nodes will be retrieved
* @param bool $direct Flag indicating whether only direct children should be retrieved
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Flag indicating whether the given node should be included in the results
*
* @return array|null List of children or null on failure
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function children($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
Expand All @@ -408,15 +414,6 @@ public function children($node = null, $direct = false, $sortByField = null, $di
return $q->getResult();
}

/**
* @param object|null $node if null, all tree nodes will be taken
* @param bool $direct true to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return QueryBuilder Query object
*/
public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
{
return $this->childrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode);
Expand All @@ -442,6 +439,8 @@ public function getChildren($node = null, $direct = false, $sortByField = null,
* @throws InvalidArgumentException if input is not valid
*
* @return QueryBuilder
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC' $direction
*/
public function getLeafsQueryBuilder($root = null, $sortByField = null, $direction = 'ASC')
{
Expand Down Expand Up @@ -494,6 +493,8 @@ public function getLeafsQueryBuilder($root = null, $sortByField = null, $directi
* @param string $direction sort direction : "ASC" or "DESC"
*
* @return Query
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC' $direction
*/
public function getLeafsQuery($root = null, $sortByField = null, $direction = 'ASC')
{
Expand All @@ -508,6 +509,8 @@ public function getLeafsQuery($root = null, $sortByField = null, $direction = 'A
* @param string $direction sort direction : "ASC" or "DESC"
*
* @return array
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC' $direction
*/
public function getLeafs($root = null, $sortByField = null, $direction = 'ASC')
{
Expand Down
4 changes: 3 additions & 1 deletion src/Tree/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public function getNodesHierarchy($node = null, $direct = false, array $options
* @param object|null $node If null, all tree nodes will be taken
* @param bool $direct True to take only direct children
* @param string|string[]|null $sortByField Field name or array of fields names to sort by
* @param string|string[] $direction Sort order ('ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return array|null List of children or null on failure
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<entity name="Gedmo\Tests\Mapping\Fixture\Xml\References" table="references">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="name" type="string" length="128"/>
<gedmo:reference field="users" reference="referenceMany" type="document" identifier="referecesId" class="Gedmo\Tests\Mapping\Fixture\Document\User" mappedBy="reference"/>
</entity>
</doctrine-mapping>
32 changes: 32 additions & 0 deletions tests/Gedmo/Mapping/Fixture/Xml/References.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Mapping\Fixture\Xml;

use Gedmo\Tests\Mapping\Fixture\Document\User;

class References
{
/**
* @var int
*/
private $id;

/**
* @var string
*/
private $name;

/**
* @var User[]
*/
private $users;
}
Loading

0 comments on commit 05e2457

Please sign in to comment.