/
githubmirror
/
magento2
Обзор
Документация
Войти
/
githubmirror
/
magento2
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
2.4-develop
lib/internal/Magento/Framework/EntityManager/Db/CreateRow.php
106 строк
3 KB
Alexandra Zota
ACP2E-4019: CE copyright updates for 2.4.9-beta1
22 окт 2025, 17:47
22 окт 2025, 17:47
1b769b7
Код
Авторство
О чём код?
<?php /** * Copyright 2016 Adobe * All Rights Reserved. */ namespace Magento\Framework\EntityManager\Db; use Exception; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\EntityManager\EntityMetadataInterface; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\App\ResourceConnection; class CreateRow { /** * @var MetadataPool */ private $metadataPool; /** * @var ResourceConnection */ private $resourceConnection; /** * CreateRow constructor. * * @param MetadataPool $metadataPool * @param ResourceConnection $resourceConnection */ public function __construct( MetadataPool $metadataPool, ResourceConnection $resourceConnection ) { $this->metadataPool = $metadataPool; $this->resourceConnection = $resourceConnection; } /** * Method to prepare data. * * @param EntityMetadataInterface $metadata * @param AdapterInterface $connection * @param array $data * @return array */ protected function prepareData(EntityMetadataInterface $metadata, AdapterInterface $connection, $data) { $output = []; foreach ($connection->describeTable($metadata->getEntityTable()) as $column) { $columnName = strtolower($column['COLUMN_NAME'] ?? ''); if ($this->canNotSetTimeStamp($columnName, $column, $data)) { continue; } if (isset($data[$columnName])) { $output[strtolower($column['COLUMN_NAME'] ?? '')] = $data[strtolower($column['COLUMN_NAME'] ?? '')]; } elseif ($column['DEFAULT'] === null) { $output[strtolower($column['COLUMN_NAME'])] = null; } } if (empty($data[$metadata->getIdentifierField()])) { $output[$metadata->getIdentifierField()] = $metadata->generateIdentifier(); } return $output; } /** * Method to can not set time stamp. * * @param string $columnName * @param string $column * @param array $data * @return bool */ private function canNotSetTimeStamp($columnName, $column, array $data) { return $column['DEFAULT'] == 'CURRENT_TIMESTAMP' && !isset($data[$columnName]) && empty($column['NULLABLE']); } /** * Method to execute. * * @param string $entityType * @param array $data * @return array * @throws Exception */ public function execute($entityType, $data) { $metadata = $this->metadataPool->getMetadata($entityType); $linkField = $metadata->getLinkField(); $entityTable = $metadata->getEntityTable(); $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName()); $connection->insert($entityTable, $this->prepareData($metadata, $connection, $data)); if (!isset($data[$linkField]) || !$data[$linkField]) { $data[$linkField] = $connection->lastInsertId($entityTable); } return $data; } }