Search code examples
phpdatabaseshopwareshopware6

Writing Data to database in shopware 6


I am trying to write data to my data table in Shopware 6 I followed the Documentation and looked everywhere and I had no luck...so my Problem here that when writing the data Shopware is telling me that the data type must be an array but when passing an array the array gets converted to bunch of INT value (automatically) I tried to insert the values as strings in a for loop instead of passing an array thinking this would fix my problem but it didn't work.

/src/Service:

class ReadingAndWritingData
{
    /** @var EntityRepository $productRepository */
    private EntityRepository $productRepository;

    private EntityRepository $deepLearningRepository;

    public function __construct(EntityRepository $productRepository,EntityRepository $deepLearningRepository)
    {
        $this->productRepository = $productRepository;
        $this->deepLearningRepository = $deepLearningRepository;
    }

    public function writeData(Context $context): void
    {
        $productId = Uuid::randomHex();
        $date = new \DateTime();
        $createdAt = $date->format('Y-m-d H:i:s');

        $productIds = $this->getProductIds($context);

            $this->deepLearningRepository->create([
                [
                    'id' => $productId,
                    'product_id' => $productIds,
                    'created_at' =>$createdAt
                ]
            ], $context);

    }

    private function getProductIds(Context $context): array
    {
        $criteria = new Criteria();

        $criteria->addFilter(new EqualsFilter('active', 1));

        return $this->productRepository->searchIds($criteria, $context)->getIds();
    }

}

/src/Core/Content/DeepLearning/DeepLearningDefention:

protected function defineFields(): FieldCollection
{
    return new FieldCollection([
        (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
        (new FkField('product_id', 'productId', ProductDefinition::class)),

        new OneToOneAssociationField('product_id', 'product_id', 'id', ProductDefinition::class, false)

    ]);
}

/src/DeepLearning:

public function activate(ActivateContext $activateContext): void
{
    $this->getCustomFieldsInstaller()->writeData($activateContext->getContext());
}

private function getCustomFieldsInstaller(): ReadingAndWritingData
{
    if ($this->container->has(ReadingAndWritingData::class)) {
        return $this->container->get(ReadingAndWritingData::class);
    }

    return new ReadingAndWritingData(
        $this->container->get('product.repository'),
        $this->container->get('deep_learning.repository')
    );
}

Solution

  • after many debuging I understood where the problem was it was expecting values that are nott null inside of the database table becuase of the asscoiaction after changing the product_id type to list it automatticaly took the values that are messing of the produc, here is How I fixed it:

    public function writeData(Context $context): void
      {
         $productsId = $this->getProductIds($context);
         
         foreach($productsId as $key => $id){
               $this->creatFunction($id, $context);
      }
    
    public function creatFunction($productId, Context $context): void
        {
            $id = Uuid::randomHex();
            $date = new \DateTime();
            $createdAt = $date->format('Y-m-d H:i:s');
        
                $this->deepLearningRepository->create([
                    [
                        'id' => $id,
                        'product_id' => [
                          'id'=>  $product_id,
                            ]
                        'created_at' =>$createdAt
                    ]
                ], $context);
    
        }