0

I am trying to develop a PHP composer app with the Slim PHP Framework, and attempted to make the changes to the migrations.php file like this

<?php

declare(strict_types=1);

require __DIR__ . '/../../src/App/App.php';

try {
    $settings = $app->getContainer()->get('settings');

    $hostname = $settings['db']['hostname'];
    $username = $settings['db']['username'];
    $password = $settings['db']['password'];
    $database = $settings['db']['database'];

    $pdo = new PDO("mysql:host=$hostname", $username, $password); //Possibly the connection
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "DROP DATABASE IF EXISTS $database";
    $pdo->exec($sql);
    echo "[OK] Database droped successfully" . PHP_EOL;

    $sql = "CREATE DATABASE $database";
    $pdo->exec($sql);
    echo "[OK] Database created successfully" . PHP_EOL;

    $sql = "USE $database";
    $pdo->exec($sql);
    echo "[OK] Database selected successfully" . PHP_EOL;

    $sql = file_get_contents(__DIR__ . '/../../database/database.sql');
    $pdo->exec($sql);
    echo "[OK] Tables created successfully" . PHP_EOL;
//    echo "[OK] Records inserted successfully" . PHP_EOL;
    $data = file_get_contents(__DIR__ . '/../../database/companies.json');
    $array = json_decode($data, true);
    foreach($array as $row) {
        $relationships = $row["relationships"];

        $pdo->exec($sql);

    }
    echo "[OK] Json inserted into tables successfully" . PHP_EOL;



} catch (PDOException $e) {
    echo "[ERROR] " . $e->getMessage() . PHP_EOL;
}

I tried loading in a companies.json file, which is 78.5MB in size and when I ran composer migration, I ran into the error Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /.../extras/bin/migration.php on line 35.

I'm still using the same composer.json in the repo

  {
    "name": "maurobonfietti/rest-api-slim-php",
    "description": "Example of REST API with Slim PHP Framework.",
    "keywords": [
        "php",
        "slim-micro-framework",
        "rest-api",
        "mysql",
        "slim3",
        "slim",
        "rest",
        "api"
    ],
    "homepage": "https://github.com/maurobonfietti/rest-api-slim-php",
    "license": "MIT",
    "authors": [
        {
            "name": "Mauro Bonfietti",
            "email": "mauro.bonfietti@gmail.com",
            "homepage": "https://github.com/maurobonfietti"
        }
    ],
    "require": {
        "firebase/php-jwt": "^5.0",
        "palanik/corsslim": "dev-slim3",
        "predis/predis": "^1.1",
        "respect/validation": "^1.1",
        "slim/slim": "^3.12.2",
        "vlucas/phpdotenv": "^2.4"
    },
    "require-dev": {
        "phpstan/phpstan": "^0.12",
        "phpunit/phpunit": "^9.0"
    },
    "config": {
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "coverage": "phpunit --coverage-html=coverage --coverage-text",
        "database": "extras/bin/restart-api.sh",
        "restart": "extras/bin/restart-api.sh",
        "migration": "php extras/bin/migration.php",
        "start": "php -S 0.0.0.0:8080 -t public public/index.php",
        "test": "phpunit"
    }
}

and I have added a php.ini file to my repository at /docker/php7/php.ini from here.

Does anyone mind telling me how to increase the memory limit for my application? I'm quite new to PHP. Let me know if more clarification is needed

Prashin Jeevaganth
  • 1,223
  • 1
  • 18
  • 42

2 Answers2

-1

I suggest you can set it via:

ini_set('memory_limit', '80M'); 

However, this is a bad solution, try not to load whole json file into memory and use something like jsonstreamingparser .

Andrii Filenko
  • 954
  • 7
  • 17
-1

It is 128M, set memory_limit = 256M for instance, hope it helps

(in .ini)

UnpassableWizard
  • 1,237
  • 11
  • 20