Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEXT-0000 - POC php attributes for entities #3688

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

OliverSkroblin
Copy link
Member

@OliverSkroblin OliverSkroblin commented Apr 26, 2024

Goal of this POC is, that you just have to define the entity class and the definition is obsolete:

<?php

namespace Shopware\Core\System\Currency;

use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\CustomFields;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Field;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\FieldType;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Fk;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\ManyToMany;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\ManyToOne;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\OnDelete;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\OneToMany;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\OneToOne;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Primary;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Protection;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Translations;
use Shopware\Core\Framework\DataAbstractionLayer\Entity as EntityStruct;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
use Shopware\Core\Framework\Struct\ArrayEntity;

#[Entity(name: 'my_entity')]
class MyEntity extends EntityStruct
{
    #[Primary]
    #[Field(type: FieldType::UUID)]
    public string $id;

    #[Field(type: FieldType::STRING)]
    public string $number;

    #[Field(type: FieldType::PRICE)]
    public ?Price $price = null;

    #[Required]
    #[Field(type: FieldType::STRING, translated: true, api: true)]
    public ?string $name = null;

    #[Field(type: FieldType::TEXT, translated: true)]
    public ?string $description = null;

    #[Protection(write: ['system'])]
    #[Field(type: FieldType::INT, translated: true)]
    public ?int $position = null;

    #[Field(type: FieldType::FLOAT, translated: true)]
    public ?float $weight = null;

    #[Field(type: FieldType::BOOL, translated: true)]
    public ?bool $highlight = null;

    #[Field(type: FieldType::DATETIME, translated: true)]
    public ?\DateTimeImmutable $release = null;

    #[Fk(entity: 'product')]
    public string $productId;

    #[Fk(entity: 'product')]
    public ?string $followId = null;

    #[ManyToOne(entity: 'product', onDelete: OnDelete::RESTRICT)]
    public ?ProductEntity $product = null;

    #[OneToOne(entity: 'product', onDelete: OnDelete::SET_NULL)]
    public ?ProductEntity $follow = null;

    /**
     * @var array<string,MySub>
     */
    #[OneToMany(entity: 'my_sub', ref: 'my_entity_id', onDelete: OnDelete::CASCADE)]
    public array $subs = [];

    /**
     * @var array<string, CategoryEntity>
     */
    #[ManyToMany(entity: 'category', onDelete: OnDelete::CASCADE)]
    public array $categories = [];

    /**
     * @var array<string, ArrayEntity>
     */
    #[Translations]
    public array $translations;

    /**
     * @var array<string, mixed>
     */
    #[CustomFields]
    public array $customFields;
}

Copy link

github-actions bot commented Apr 26, 2024

Warnings
⚠️ The Pull Request doesn't contain any changelog file
⚠️ Please be kind and add unit tests for your new code in these files:
src/Core/Framework/DataAbstractionLayer/Attribute/CustomFields.php
src/Core/Framework/DataAbstractionLayer/Attribute/FieldType.php
src/Core/Framework/DataAbstractionLayer/Attribute/Fk.php
src/Core/Framework/DataAbstractionLayer/Attribute/Inherited.php
src/Core/Framework/DataAbstractionLayer/Attribute/ManyToMany.php
src/Core/Framework/DataAbstractionLayer/Attribute/ManyToOne.php
src/Core/Framework/DataAbstractionLayer/Attribute/OnDelete.php
src/Core/Framework/DataAbstractionLayer/Attribute/OneToMany.php
src/Core/Framework/DataAbstractionLayer/Attribute/OneToOne.php
src/Core/Framework/DataAbstractionLayer/Attribute/Primary.php
src/Core/Framework/DataAbstractionLayer/Attribute/Protection.php
src/Core/Framework/DataAbstractionLayer/Attribute/ReferenceVersion.php
src/Core/Framework/DataAbstractionLayer/Attribute/Required.php
src/Core/Framework/DataAbstractionLayer/Attribute/Serialized.php
src/Core/Framework/DataAbstractionLayer/Attribute/Translations.php
src/Core/Framework/DataAbstractionLayer/Attribute/Version.php
src/Core/Framework/DataAbstractionLayer/Field/Flag/AsArray.php
src/Core/Framework/DependencyInjection/CompilerPass/AttributeEntityCompilerPass.php
If you are sure everything is fine with your changes, you can resolve this warning.
You can run `composer make:coverage` to generate dummy unit tests for files that are not covered

@OliverSkroblin OliverSkroblin force-pushed the next-0000/entity-via-attribute branch from 6c05c38 to f564ce7 Compare May 14, 2024 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant