|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / vendor / magento / framework / EntityManager / Db / |
Filename | /home/dev2.destoffenstraat.com/vendor/magento/framework/EntityManager/Db/CreateRow.php |
Size | 2.94 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 07-Jan-2021 21:08 |
Last accessed | 22-Aug-2025 22:23 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\EntityManager\Db;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\EntityManager\EntityMetadataInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\App\ResourceConnection;
/**
* Class CreateRow
*/
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;
}
/**
* @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;
}
/**
* @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']);
}
/**
* @param string $entityType
* @param array $data
* @return array
*/
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;
}
}
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\EntityManager\Db;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\EntityManager\EntityMetadataInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\App\ResourceConnection;
/**
* Class CreateRow
*/
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;
}
/**
* @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;
}
/**
* @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']);
}
/**
* @param string $entityType
* @param array $data
* @return array
*/
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;
}
}