Skip to content

Commit

Permalink
POC entity attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSkroblin committed May 13, 2024
1 parent 4a2e40f commit 6c05c38
Show file tree
Hide file tree
Showing 33 changed files with 1,114 additions and 13 deletions.
62 changes: 62 additions & 0 deletions custom/scripts/foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Scripts\Examples;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Uuid\Uuid;

require_once __DIR__ . '/examples/base-script.php';

$env = 'dev'; // by default, kernel gets booted in dev

$kernel = require __DIR__ . '/boot/boot.php';

class Main extends BaseScript
{
public function run()
{
$this->getContainer()->get(Connection::class)->executeStatement(
'DELETE FROM my_entity WHERE 1 = 1'
);

$definition = $this->getContainer()->get('my_entity.definition');

/** @var EntityRepository $repo */
$repo = $this->getContainer()->get('my_entity.repository');

$data = [
[
'id' => Uuid::randomHex(),
'name' => 'foo',
'number' => 'foo',
'productId' => '018f7142ea7872f4a675f0eb497f190b',
'followId' => '018f7142ea7872f4a675f0eb497f190b',
'categories' => [
['id' => '018f71422f4472bfadf392a3eba2e34f']
],
'subs' => [
[
'number' => 'foo'
]
]
]
];

$repo->upsert($data, Context::createCLIContext());

$criteria = new Criteria();
$criteria->addAssociation('categories');
$criteria->addAssociation('follow');
$criteria->addAssociation('product');
$criteria->addAssociation('subs');
$entities = $repo->search($criteria, Context::createCLIContext());

file_put_contents(__DIR__ . '/my_entity.json', json_encode($entities, JSON_PRETTY_PRINT));
}
}


(new Main($kernel))->run();
24 changes: 12 additions & 12 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1713968789,
"lastModified": 1715346321,
"owner": "cachix",
"repo": "devenv",
"rev": "b26b52a4dac68bdc305f6b9df948c97f49b2c3ee",
"treeHash": "4a034bbd3511c196f4075a1eb0da1b422d1011db",
"rev": "61033d861900f36bfa6afcd5b8580f098d65e137",
"treeHash": "8ed36d3a3715ded632a1442d07122aba44719fde",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -74,11 +74,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1713968665,
"lastModified": 1715499532,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "a0cadba85e907b8e2d6ae549d25535da3b1b6d5e",
"treeHash": "0751b1ac3399476c055779f35d47e5f5a83921de",
"rev": "af8b9db5c00f1a8e4b83578acc578ff7d823b786",
"treeHash": "ad94ed0880ee4eda683cf5f52c7a3f2bbb3c712a",
"type": "github"
},
"original": {
Expand All @@ -90,11 +90,11 @@
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1713828541,
"lastModified": 1715395895,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b500489fd3cf653eafc075f9362423ad5cdd8676",
"treeHash": "36037c2cc4ca42960ec785fb7eea87289846dd21",
"rev": "71bae31b7dbc335528ca7e96f479ec93462323ff",
"treeHash": "b8ae138025f110a8e468a9fe095084da6fd07c91",
"type": "github"
},
"original": {
Expand All @@ -115,11 +115,11 @@
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1713954846,
"lastModified": 1714478972,
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "6fb82e44254d6a0ece014ec423cb62d92435336f",
"treeHash": "a456512c8da29752b79131f1e5b45053e2394078",
"rev": "2849da033884f54822af194400f8dff435ada242",
"treeHash": "578180deb59a545b0032e9a66da4c0c043c5057d",
"type": "github"
},
"original": {
Expand Down
16 changes: 16 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
class Entity
{
/**
* @var class-string
*/
public string $class;

public function __construct(public string $name) {}
}


15 changes: 15 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Field
{
public bool $nullable;

public function __construct(
public string $type,
public bool $translated = false,
public bool|array $api = false
) {}
}
27 changes: 27 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/FieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

enum FieldType: string
{
public const STRING = 'string';
public const TEXT = 'text';
public const INT = 'int';
public const FLOAT = 'float';
public const BOOL = 'bool';
public const DATETIME = 'datetime';
public const PRICE = 'price';
public const UUID = 'uuid';
public const AUTO_INCREMENT = 'auto-increment';
public const CUSTOM_FIELDS = 'custom-fields';

public const SERIALIZED = 'serialized';
public const JSON = 'json';
public const DATE = 'date';
public const DATE_INTERVAL = 'date-interval';
public const EMAIL = 'email';
public const LIST = 'list';
public const PASSWORD = 'password';
public const REMOTE_ADDRESS = 'remote-address';
public const TIME_ZONE = 'time-zone';
}
16 changes: 16 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Fk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Fk extends Field
{
public const TYPE = 'fk';

public bool $nullable;

public function __construct(public string $entity, public bool|array $api = false)
{
parent::__construct(type: self::TYPE, api: $api);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Inherited
{
public function __construct(public bool $reversed = false) {}
}
14 changes: 14 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/ManyToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ManyToMany extends Field
{
public const TYPE = 'many-to-many';

public function __construct(public string $entity, public bool|array $api = false)
{
parent::__construct(type: self::TYPE, api: $api);
}
}
18 changes: 18 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/ManyToOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ManyToOne extends Field
{
public const TYPE = 'many-to-one';

public function __construct(
public string $entity,
public string $ref = 'id' ,
public bool|array $api = false
) {
parent::__construct(type: self::TYPE,api: $api);

}
}
13 changes: 13 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/OnDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OnDelete
{
public const CASCADE = 'cascade';
public const SET_NULL = 'set-null';
public const RESTRICT = 'restrict';

public function __construct(public string $behavior) {}
}
17 changes: 17 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/OneToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OneToMany extends Field
{
public const TYPE = 'one-to-many';

public function __construct(
public string $entity,
public string $ref,
public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
17 changes: 17 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/OneToOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OneToOne extends Field
{
public const TYPE = 'one-to-one';

public function __construct(
public string $entity,
public ?string $column = null,
public string $ref = 'id', public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
9 changes: 9 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Primary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Primary
{
public function __construct() {}
}
15 changes: 15 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Protection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

use Shopware\Core\Framework\Context;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Protection
{
final public const SYSTEM_SCOPE = Context::SYSTEM_SCOPE;
final public const USER_SCOPE = Context::USER_SCOPE;
final public const CRUD_API_SCOPE = Context::CRUD_API_SCOPE;

public function __construct(public array $write) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ReferenceVersion extends Field
{
public const TYPE = 'reference-version';

public function __construct(public string $entity)
{
parent::__construct(type: self::TYPE, api: true);
}
}
11 changes: 11 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Required.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Required
{
public function __construct()
{
}
}
18 changes: 18 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Serialized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\StringFieldSerializer;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Serialized extends Field
{
public const TYPE = 'serialized';

public function __construct(
public string $serializer = StringFieldSerializer::class,
public bool|array $api = false
) {
parent::__construct(type: self::TYPE, api: $api);
}
}
14 changes: 14 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Translations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Translations extends Field
{
public const TYPE = 'translations';

public function __construct()
{
parent::__construct(type: self::TYPE, api: true);
}
}
14 changes: 14 additions & 0 deletions src/Core/Framework/DataAbstractionLayer/Attribute/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shopware\Core\Framework\DataAbstractionLayer\Attribute;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Version extends Field
{
public const TYPE = 'version';

public function __construct()
{
parent::__construct(type: self::TYPE, api: true);
}
}

0 comments on commit 6c05c38

Please sign in to comment.