Skip to content

Commit

Permalink
NEXT-1069 - Fix loading nested associations
Browse files Browse the repository at this point in the history
  • Loading branch information
janbuecker committed Nov 15, 2018
1 parent 6b590af commit 343b597
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 16 deletions.
Expand Up @@ -15,6 +15,8 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ObjectField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ReferenceVersionField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\TenantIdField;
Expand Down Expand Up @@ -58,12 +60,13 @@ public static function defineFields(): FieldCollection
(new FloatField('unit_price', 'unitPrice'))->setFlags(new Required()),
(new FloatField('total_price', 'totalPrice'))->setFlags(new Required()),
new StringField('description', 'description'),
new IdField('parent_id', 'parentId'),
new ParentField(self::class),
new StringField('type', 'type'),
new CreatedAtField(),
new UpdatedAtField(),
new ManyToOneAssociationField('order', 'order_id', OrderDefinition::class, false),
(new OneToManyAssociationField('orderDeliveryPositions', OrderDeliveryPositionDefinition::class, 'order_line_item_id', false, 'id'))->setFlags(new CascadeDelete(), new ReadOnly()),
new ParentAssociationField(self::class, false),
]);
}

Expand Down
17 changes: 16 additions & 1 deletion Checkout/Order/Aggregate/OrderLineItem/OrderLineItemStruct.php
Expand Up @@ -110,6 +110,11 @@ class OrderLineItemStruct extends Entity
*/
protected $orderDeliveryPositions;

/**
* @var OrderLineItemStruct|null
*/
protected $parent;

public function getOrderId(): string
{
return $this->orderId;
Expand Down Expand Up @@ -255,7 +260,7 @@ public function getParentId(): ?string
return $this->parentId;
}

public function setParentId(?string $parentId): void
public function setParentId(string $parentId): void
{
$this->parentId = $parentId;
}
Expand Down Expand Up @@ -309,4 +314,14 @@ public function setOrderDeliveryPositions(OrderDeliveryPositionCollection $order
{
$this->orderDeliveryPositions = $orderDeliveryPositions;
}

public function getParent(): ?OrderLineItemStruct
{
return $this->parent;
}

public function setParent(OrderLineItemStruct $parent): void
{
$this->parent = $parent;
}
}
4 changes: 2 additions & 2 deletions Content/Category/CategoryDefinition.php
Expand Up @@ -20,6 +20,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ReferenceVersionField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
Expand Down Expand Up @@ -51,7 +52,6 @@ public static function defineFields(): FieldCollection
new VersionField(),
new CatalogField(),

new FkField('parent_id', 'parentId', self::class),
new ParentField(self::class),
new ReferenceVersionField(self::class, 'parent_version_id'),

Expand Down Expand Up @@ -82,7 +82,7 @@ public static function defineFields(): FieldCollection
new TranslatedField('metaDescription'),
new TranslatedField('cmsHeadline'),
new TranslatedField('cmsDescription'),
new ManyToOneAssociationField('parent', 'parent_id', self::class, false),
new ParentAssociationField(self::class, false),
new ManyToOneAssociationField('media', 'media_id', MediaDefinition::class, false),
(new ChildrenAssociationField(self::class))->setFlags(new CascadeDelete()),
(new TranslationsAssociationField(CategoryTranslationDefinition::class))->setFlags(new Required(), new CascadeDelete()),
Expand Down
6 changes: 4 additions & 2 deletions Content/Product/ProductDefinition.php
Expand Up @@ -29,6 +29,8 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceRulesJsonField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ReferenceVersionField;
Expand Down Expand Up @@ -75,7 +77,7 @@ public static function defineFields(): FieldCollection
new VersionField(),
new CatalogField(),

new FkField('parent_id', 'parentId', self::class),
new ParentField(self::class),
new ReferenceVersionField(self::class, 'parent_version_id'),

(new IntField('auto_increment', 'autoIncrement'))->setFlags(new ReadOnly()),
Expand Down Expand Up @@ -141,7 +143,7 @@ public static function defineFields(): FieldCollection
(new TranslatedField('packUnit'))->setFlags(new Inherited()),

//parent - child inheritance
new ManyToOneAssociationField('parent', 'parent_id', self::class, false),
new ParentAssociationField(self::class, false),
new ChildrenAssociationField(self::class),

//inherited associations
Expand Down
11 changes: 10 additions & 1 deletion Framework/DataAbstractionLayer/Dbal/EntityReader.php
Expand Up @@ -13,6 +13,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\SearchKeywordAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslatedField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
Expand Down Expand Up @@ -190,7 +191,7 @@ private function joinBasic(string $definition, Context $context, string $root, Q
}

//self references can not be resolved, otherwise we get an endless loop
if ($field instanceof AssociationInterface && $field->getReferenceClass() === $definition) {
if ($field instanceof AssociationInterface && $field->getReferenceClass() === $definition && !$field instanceof ParentAssociationField) {
continue;
}

Expand Down Expand Up @@ -769,6 +770,10 @@ private function loadManyToManyWithCriteria(Criteria $fieldCriteria, ManyToManyA

$read = new ReadCriteria($ids);

foreach ($fieldCriteria->getAssociations() as $fieldName => $associationCriteria) {
$read->addAssociation($fieldName, $associationCriteria);
}

$referenceClass = $association->getReferenceDefinition();
$collectionClass = $referenceClass::getCollectionClass();
$data = $this->_read($read, $referenceClass, $context, $referenceClass::getStructClass(), new $collectionClass(), $referenceClass::getFields()->filterBasic());
Expand Down Expand Up @@ -885,6 +890,10 @@ private function hasCriteriaElements(Criteria $criteria): bool
return true;
}

if (!empty($criteria->getAssociations())) {
return true;
}

return false;
}

Expand Down
7 changes: 1 addition & 6 deletions Framework/DataAbstractionLayer/EntityDefinition.php
Expand Up @@ -168,11 +168,6 @@ public static function getDefaults(EntityExistence $existence): array
return [];
}

public static function allowInheritance(): bool
{
return false;
}

public static function isChildrenAware(): bool
{
return static::getFields()->get('children') instanceof ChildrenAssociationField;
Expand All @@ -185,7 +180,7 @@ public static function isChildCountAware(): bool

public static function isInheritanceAware(): bool
{
return static::allowInheritance() && static::getFields()->get('parent') instanceof ManyToOneAssociationField;
return false;
}

public static function isVersionAware(): bool
Expand Down
14 changes: 14 additions & 0 deletions Framework/DataAbstractionLayer/Field/ParentAssociationField.php
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\Field;

class ParentAssociationField extends ManyToOneAssociationField
{
public function __construct(
string $referenceClass,
bool $loadInBasic,
string $referenceField = 'id'
) {
parent::__construct('parent', 'parent_id', $referenceClass, $loadInBasic, $referenceField);
}
}
1 change: 0 additions & 1 deletion Framework/DataAbstractionLayer/Field/ParentField.php
Expand Up @@ -7,6 +7,5 @@ class ParentField extends FkField
public function __construct(string $referenceClass)
{
parent::__construct('parent_id', 'parentId', $referenceClass);
$this->referenceClass = $referenceClass;
}
}
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer;

use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField;

class ParentAssociationFieldSerializer extends ManyToOneAssociationFieldSerializer
{
public function getFieldClass(): string
{
return ParentAssociationField::class;
}
}
5 changes: 5 additions & 0 deletions Framework/DependencyInjection/data-abstraction-layer.xml
Expand Up @@ -1173,5 +1173,10 @@
<service class="Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\VersionFieldSerializer" id="Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\VersionFieldSerializer">
<tag name="shopware.field_serializer" />
</service>

<service class="Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\ParentAssociationFieldSerializer" id="Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\ParentAssociationFieldSerializer">
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Write\WriteCommandExtractor"/>
<tag name="shopware.field_serializer" />
</service>
</services>
</container>
Expand Up @@ -681,7 +681,6 @@ public function testLoadManyToManyNotLoadedAutomatically(): void

public function testLoadNestedAssociation(): void
{
static::markTestIncomplete('this test should be green if NEXT-1069 is resolved');
$id1 = Uuid::uuid4()->getHex();
$id2 = Uuid::uuid4()->getHex();

Expand Down
3 changes: 2 additions & 1 deletion System/Language/LanguageDefinition.php
Expand Up @@ -23,6 +23,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ParentField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\TenantIdField;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static function defineFields(): FieldCollection
(new StringField('name', 'name'))->setFlags(new Required()),
new CreatedAtField(),
new UpdatedAtField(),
new ManyToOneAssociationField('parent', 'parent_id', LanguageDefinition::class, false),
new ParentAssociationField(self::class, false),
new ManyToOneAssociationField('locale', 'locale_id', LocaleDefinition::class, true),
new ChildrenAssociationField(self::class),
new OneToManyAssociationField('salesChannelDefaultAssignments', SalesChannelDefinition::class, 'language_id', false, 'id'),
Expand Down

0 comments on commit 343b597

Please sign in to comment.