According to this page it is now possible and recommended to define entities without the use of getters and setters for the fields.
But when I define my Entity like this in "shopware/core": "~v6.6.9.0",
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Field;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\FieldType;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\ManyToMany;
class ProductBundleEntity extends Entity
{
#[PrimaryKey]
#[Field(type: FieldType::UUID)]
public string $id;
#[Field(type: FieldType::FLOAT)]
public ?float $discount;
#[Field(type: FieldType::STRING)]
public ?string $discountType;
#[Field(type: FieldType::BOOL)]
public bool $active;
#[Field(type: FieldType::DATETIME)]
public ?\DateTimeInterface $validFrom;
#[Field(type: FieldType::DATETIME)]
public ?\DateTimeInterface $validTo;
/**
* @var array<string, ProductEntity>|null
*/
#[ManyToMany(
entity: 'product',
)]
public ?array $products = null;
}
and try to validate using bin/console dal:validate
, I get a bunch of errors:
* No getter function for property id
* No setter function for property id
* No getter function for property discount
* No setter function for property discount
* No getter function for property discountType
* No setter function for property discountType
* No getter function for property active
* No setter function for property active
* No getter function for property validFrom
* No setter function for property validFrom
* No getter function for property validTo
* No setter function for property validTo
* No getter function for property products
* No setter function for property products
Is that just not implemented yet or am I doing something wrong?
According to this page it is now possible and recommended to define entities without the use of getters and setters for the fields.
But when I define my Entity like this in "shopware/core": "~v6.6.9.0",
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\Field;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\FieldType;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Attribute\ManyToMany;
class ProductBundleEntity extends Entity
{
#[PrimaryKey]
#[Field(type: FieldType::UUID)]
public string $id;
#[Field(type: FieldType::FLOAT)]
public ?float $discount;
#[Field(type: FieldType::STRING)]
public ?string $discountType;
#[Field(type: FieldType::BOOL)]
public bool $active;
#[Field(type: FieldType::DATETIME)]
public ?\DateTimeInterface $validFrom;
#[Field(type: FieldType::DATETIME)]
public ?\DateTimeInterface $validTo;
/**
* @var array<string, ProductEntity>|null
*/
#[ManyToMany(
entity: 'product',
)]
public ?array $products = null;
}
and try to validate using bin/console dal:validate
, I get a bunch of errors:
* No getter function for property id
* No setter function for property id
* No getter function for property discount
* No setter function for property discount
* No getter function for property discountType
* No setter function for property discountType
* No getter function for property active
* No setter function for property active
* No getter function for property validFrom
* No setter function for property validFrom
* No getter function for property validTo
* No setter function for property validTo
* No getter function for property products
* No setter function for property products
Is that just not implemented yet or am I doing something wrong?
Share Improve this question edited Mar 27 at 9:03 Tom M asked Mar 25 at 17:22 Tom MTom M 2,9062 gold badges23 silver badges48 bronze badges1 Answer
Reset to default 1In Shopware 6, the bin/console dal:validate
command is implemented in the DataAbstractionLayerValidateCommand
class.
When this command is executed, it triggers the execute()
method. During execution, the method utilizes the DefinitionValidator
service to run the main validation process for all registered Entity Definitions.
One of the key steps in this validation process is calling the following code:
$errors = $this->validator->validate();
As part of this process, it invokes the validateStruct()
method to validate the structure of the entities, checking for the presence of necessary getter and setter methods.
Here’s the relevant code inside the validate()
method:
$violations[$definitionClass] = array_merge(
$violations[$definitionClass],
$this->validateStruct($struct, $definition)
);
The validateStruct()
method ensures that all expected getter and setter methods are present in the Entity Struct. It checks whether each field in the definition has a corresponding getter and setter method, and if not, it generates errors like:
No getter function for property xyz in App\Entity\ExampleStruct
No setter function for property xyz in App\Entity\ExampleStruct
The issue you are facing is related to how the bin/console dal:validate
command works. It expects explicit getters and setters for each entity property, which leads to errors when using attributes like #[Field]
and #[ManyToMany]
without the corresponding methods. To resolve this issue, you can either explicitly define getters and setters for all properties or wait for Shopware to update the validation logic so the command works correctly with entity attributes.