我已经在Sylius 1.3.1中覆盖了\Sylius\Component\Core\Model\ProductVariant
和\Sylius\Component\Product\Model\ProductAttribute
。但是当我覆盖\Sylius\Component\Core\Model\Product
时,我的测试失败了:
Doctrine\DBAL\Exception\NotNullConstraintViolationException: An exception occurred while executing 'INSERT INTO sylius_product_variant (code, created_at, updated_at, position, product_id, on_hold, on_hand, tracked, width, height, depth, weight, shipping_required, tax_category_id, shipping_category_id, configuration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["LOGAN_MUG_CODE", "2018-12-06 02:10:27", "2018-12-06 02:10:33", 0, null, 0, 0, 0, null, null, null, null, 1, null, null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null
/var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:123
/var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:184
/var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:158
/var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:178
/var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:285
/var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1073
/var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:386
/var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:359
/var/www/html/var/cache/test/Container26IvKi6/EntityManager_9a5be93.php:83
/var/www/html/vendor/theofidry/alice-data-fixtures/src/Bridge/Doctrine/Persister/ObjectManagerPersister.php:103
/var/www/html/var/cache/test/Container26IvKi6/ObjectManagerPersister_eccc052.php:18
/var/www/html/vendor/theofidry/alice-data-fixtures/src/Loader/PersisterLoader.php:91
/var/www/html/var/cache/test/Container26IvKi6/PersisterLoader_c8a8e24.php:18
/var/www/html/vendor/theofidry/alice-data-fixtures/src/Loader/PurgerLoader.php:119
/var/www/html/var/cache/test/Container26IvKi6/PurgerLoader_cb68499.php:18
/var/www/html/vendor/lakion/api-test-case/src/ApiTestCase.php:305
/var/www/html/tests/Api/ProductApiTest.php:16
Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null
我已经检查过How to override Product model in Sylius?,但是这个问题不适合我的情况。
要覆盖提到的实体,我做了以下工作:
Product.php
declare(strict_types=1);
namespace App\Entity;
class Product extends \Sylius\Component\Core\Model\Product
{
const STATUS_UNKNOWN = 0;
const STATUS_AVAILABLE = 1;
const STATUS_OBSOLETE = 2;
public function __construct()
{
parent::__construct();
}
/**
* @var int
*/
private $status = self::STATUS_AVAILABLE;
/**
* @return int
*/
public function getStatus(): int
{
return $this->status;
}
/**
* @param int $status
*/
public function setStatus(int $status): void
{
$this->status = $status;
}
}
ProductVariant.php
declare(strict_types=1);
namespace App\Entity;
class ProductVariant extends \Sylius\Component\Core\Model\ProductVariant
{
/**
* @var array
*/
private $configuration;
/**
* @return array|null
*/
public function getConfiguration(): ?array
{
return $this->configuration;
}
/**
* @param array $configuration
*/
public function setConfiguration(array $configuration): void
{
$this->configuration = $configuration;
}
}
ProductAttribute.php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Channel\Model\ChannelInterface;
class ProductAttribute extends \Sylius\Component\Product\Model\ProductAttribute
{
/**
* @var Collection|ChannelInterface[]
*/
protected $channels;
public function __construct()
{
parent::__construct();
$this->channels = new ArrayCollection();
}
/**
* {@inheritdoc}
*/
public function getChannels(): Collection
{
return $this->channels;
}
/**
* {@inheritdoc}
*/
public function addChannel(ChannelInterface $channel): void
{
if (!$this->hasChannel($channel)) {
$this->channels->add($channel);
}
}
/**
* {@inheritdoc}
*/
public function removeChannel(ChannelInterface $channel): void
{
if ($this->hasChannel($channel)) {
$this->channels->removeElement($channel);
}
}
/**
* {@inheritdoc}
*/
public function hasChannel(ChannelInterface $channel): bool
{
return $this->channels->contains($channel);
}
}
Product.orm.yml
App\Entity\Product:
type: entity
table: sylius_product
fields:
status:
type: integer
nullable: false
ProductVariant.orm.yml
App\Entity\ProductVariant:
type: entity
table: sylius_product_variant
fields:
configuration:
type: json
nullable: true
ProductAttribute.orm.yml
App\Entity\ProductAttribute:
type: entity
table: sylius_product_attribute
manyToMany:
channels:
targetEntity: Sylius\Component\Channel\Model\ChannelInterface
joinTable:
name: sylius_product_attribute_channel
joinColumns:
attribute_id:
referencedColumnName: id
nullable: false
onDelete: CASCADE
inverseJoinColumns:
channel_id:
referencedColumnName: id
nullable: false
onDelete: CASCADE
sylius.yaml
parameters:
sylius_core.public_dir: '%kernel.project_dir%/public'
sylius_attribute:
resources:
product:
subject: '%sylius.model.product.class%'
attribute:
classes:
model: App\Entity\ProductAttribute
sylius_product:
resources:
product_variant:
classes:
model: App\Entity\ProductVariant
product:
classes:
model: App\Entity\Product
我认为在App/Entity/Product
中为Doctrine\ORM\UnitOfWork::getCommitOrder()
生成的依赖关系图不正确。所以我的问题必须出在配置上。
我在这里迷路了。有人可以给我一个提示吗?
答案 0 :(得分:0)
问题是如此简单:rm -r var/cache
之后,它开始工作了。长话短说:清除缓存!