1

I am getting warning fetchall can be memory inefficient for large data sets. its working fine but not for MEQP2 its giving WARNING so how use alternate of fetchall() in the code

namespace Marstranslation\Marstranslation\Model\ResourceModel;

class TranslatableEntity implements \Marstranslation\Marstranslation\Api\Data\TranslatableEntityInterface
{
    private $resource;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
    ) {
        $this->resource = $resource;
    }
    public function getConfig($configPath)
    {
        $connection = $this->resource->getConnection(
            \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION
        );
        $select = $connection->select()->from(
            $connection->getTableName(
                'core_config_data'
            ),
            ['value']
        )
        ->where(
            'path = ?',
            $configPath
        );
        $result = $connection->fetchOne($select);
        return ($result) ? $result:null;
    }
    public function setConfig(
        $configPath,
        $value
    ) {
        $connection = $this->resource->getConnection(
            \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION
        );
        try {
            $connection->beginTransaction();
            $connection->insertOnDuplicate(
                $connection->getTableName('core_config_data'),
                ['path' => $configPath, 'value' => $value],
                ['path', 'value']
            );
            $connection->commit();
        } catch (\Exception $ex) {
            $connection->rollBack();
        }
    }

    public function getTranslationStatusByProductId($id)
    {
        $connection = $this->resource->getConnection();
        $select = $connection->select()
            ->from(
                [
                    'mp' => $this->resource->getTableName(
                        \Marstranslation\Marstranslation\Model\ConstantsInterface::
                        MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                    )
                ],
                ['mp.source_product_id', 'mp.order_status', 'mp.project_id']
            );
        $result = $connection->fetchAll($select);
        foreach ($result as $item) {
            if (in_array($id, explode(',', $item['source_product_id']))) {
                return $item['order_status'];
            }
        }
        return 0;
    }

    public function getMarsOrderIdByMagentoProductId($id)
    {
        $connection = $this->resource->getConnection();
        $select = $connection->select()
            ->from(
                [
                    'mp' => $this->resource->getTableName(
                        \Marstranslation\Marstranslation\Model\ConstantsInterface::
                        MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                    )
                ],
                ['mp.source_product_id', 'mp.project_id']
            );
        $result = $connection->fetchAll($select);
        foreach ($result as $item) {
            if (in_array($id, explode(',', $item['source_product_id']))) {
                return $item['project_id'];
            }
        }
        return -1;
    }

    public function getAllTaskIdsOfParticularMarsOrder($orderId)
    {
        $connection = $this->resource->getConnection();
        $select = $connection->select()
            ->from(
                [
                    'mp' => $this->resource->getTableName(
                        \Marstranslation\Marstranslation\Model\ConstantsInterface::
                        MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                    )
                ],
                ['mp.task_id']
            )->where('mp.project_id = ?', $orderId);
        return $connection->fetchAll($select);
    }

    public function updateTaskStatus($connection, $data)
    {
        foreach ($data['task_status'] as $taskId => $taskStatus) {
            $connection->update(
                $this->resource->getTableName(
                    \Marstranslation\Marstranslation\Model\ConstantsInterface::
                    MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                ),
                ['status' => $taskStatus],
                ['task_id = ?' => $taskId]
            );
        }
    }

    public function updateOrderStatus($connection, $data)
    {
        foreach ($data['order_status'] as $taskId => $orderStatus) {
            $connection->update(
                $this->resource->getTableName(
                    \Marstranslation\Marstranslation\Model\ConstantsInterface::
                    MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                ),
                ['order_status' => $orderStatus],
                ['task_id = ?' => $taskId]
            );
        }
    }

    public function updateFilePath($connection, $fileType, $taskId, $filePath)
    {
        $connection->update(
            $this->resource->getTableName(
                \Marstranslation\Marstranslation\Model\ConstantsInterface::
                MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
            ),
            [$fileType => $filePath],
            ['task_id = ?' => $taskId]
        );
    }

    public function updateMarsOrdersByTaskId($orderData)
    {
        $connection = $this->resource->getConnection();
        foreach ($orderData as $orderId => $data) {
            $this->updateOrderStatus($connection, $data);
            $this->updateTaskStatus($connection, $data);
            if (isset($data['files'])) {
                foreach ($data['files'] as $fileType => $file) {
                    if ($fileType == 'translation_file') {
                        foreach ($file as $taskId => $location) {
                            $this->updateFilePath(
                                $connection,
                                'translated_file_path',
                                $taskId,
                                $location
                            );
                        }
                    }
                    if ($fileType == 'proofreader_file') {
                        foreach ($file as $taskId => $location) {
                            $this->updateFilePath(
                                $connection,
                                'proofreader_file_path',
                                $taskId,
                                $location
                            );
                        }
                    }
                }
            }
        }
    }

    public function updateOrderStatusWhenAllTasksAreCompleted($orderId)
    {
        $connection = $this->resource->getConnection();
        $select = $connection->select()
            ->from(
                [
                    'mp' => $this->resource->getTableName(
                        \Marstranslation\Marstranslation\Model\ConstantsInterface::
                        MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                    )
                ],
                ['mp.status']
            )
            ->where('mp.project_id = ?', $orderId);
        $connection->fetchAll($select);
    }

    public function getMagentoSourceAndTargetStoreLocaleBySourceProductId($productId)
    {
        $connection = $this->resource->getConnection();
        $select = $connection->select()
            ->from(
                [
                    'mp' => $this->resource->getTableName(
                        \Marstranslation\Marstranslation\Model\ConstantsInterface::
                        MARSTRANSLATION_MAGENTO_DB_TABLE_NAME
                    )
                ],
                [
                    'mp.source_product_id', 'mp.magento_store_id_origin',
                    'mp.magento_store_id_translation', 'mp.translated_file_path'
                ]
            );
        $result = $connection->fetchAll($select);
        $data = [];
        foreach ($result as $item) {
            if (in_array($productId, explode(',', $item['source_product_id']))) {
                $data[] = $item['magento_store_id_origin'] . '~' .
                    $item['magento_store_id_translation'] . '~' . $item['translated_file_path'];
            }
        }
        return $data;
    }
}
1

You can use magento's implementation of a query generator function Magento\Framework\DB\Query\Generator by default it will limit the results to 100, but you can change it.

Here is more how less how you would do it:

$batchSelectIterator = $this->batchQueryGenerator->generate(
     'value_id',
     $connection->select()
          ->from(
               //
          )->join(
               //
          )->where(
               //
          ),
         [YOUR_CUSTOM_BATCH_LIMIT]
      );

// Yield is what does the trick here :)
foreach ($batchSelectIterator as $select) {
     yield $connection->fetchAll($select);
}
  • i just change fetch all to fetchone and the warning solved. but thankx for your answer. – M RIZWAN Mar 8 '18 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.