jeudi 25 août 2016

Laravel Migration - Model::find($id) return NULL but $id exists in database

I have a curious problem with an really small migration.

Basic information: I currently have 3 instances of my Project: "Dev PC" (offline), "Dev Laptop" (offline) and "QS" (online)

So the last two days I wrote three different migrations. One wich imports an JSON file with information about 6.000 Instances of the Model and write those Models to DB.

Second (wich now is failing) should write some new data to those models and third one will rewrite some data.

If this is perfect should not be discussed here. That doesn't mind!

So heres the Problem: The second migration is failing on QS stage, but working on both Dev stages.

This was the first error

-sh-4.2$ /opt/plesk/php/5.6/bin/php artisan migrate
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > yes



  [ErrorException]
  Creating default object from empty value

Then I tryed around a bit

-sh-4.2$ /opt/plesk/php/5.6/bin/php artisan migrate --force --pretend -vvv
6219

  [ErrorException]
  Creating default object from empty value


Exception trace:
 () at /var/www/vhosts/database/migrations/2016_08_25_080129_import_old_adresses.php:28
 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a
 call_user_func() at /var/www/vhosts/vendor/sentry/sentry/lib/Raven/Breadcrumbs/ErrorHandler.php:36
 Raven_Breadcrumbs_ErrorHandler->handleError() at /var/www/vhosts/database/migrations/2016_08_25_080129_import_old_adresses.php:28
 ImportOldAdresses->up() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:319
 Illuminate\Database\Migrations\Migrator->Illuminate\Database\Migrations\{closure}() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Connection.php:656
 Illuminate\Database\Connection->pretend() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:320
 Illuminate\Database\Migrations\Migrator->getQueries() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:295
 Illuminate\Database\Migrations\Migrator->pretendToRun() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:144
 Illuminate\Database\Migrations\Migrator->runUp() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:117
 Illuminate\Database\Migrations\Migrator->runMigrationList() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:86
 Illuminate\Database\Migrations\Migrator->run() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:77
 Illuminate\Database\Console\Migrations\MigrateCommand->fire() at n/a:n/a
 call_user_func_array() at /var/www/vhosts/bootstrap/cache/compiled.php:1257
 Illuminate\Container\Container->call() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Console/Command.php:169
 Illuminate\Console\Command->execute() at /var/www/vhosts/vendor/symfony/console/Command/Command.php:256
 Symfony\Component\Console\Command\Command->run() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Console/Command.php:155
 Illuminate\Console\Command->run() at /var/www/vhosts/vendor/symfony/console/Application.php:794
 Symfony\Component\Console\Application->doRunCommand() at /var/www/vhosts/vendor/symfony/console/Application.php:186
 Symfony\Component\Console\Application->doRun() at /var/www/vhosts/vendor/symfony/console/Application.php:117
 Symfony\Component\Console\Application->run() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:107
 Illuminate\Foundation\Console\Kernel->handle() at /var/www/vhosts/artisan:36

And now I wanted to see the SQLs wich where fired:

-sh-4.2$ /opt/plesk/php/5.6/bin/php artisan migrate --force --pretend -vvv
array:2 [
  0 => array:3 [
    "query" => "alter table `orders` add `oldName` varchar(255) not null, add `oldAdress` varchar(255) not null"
    "bindings" => []
    "time" => 0.0
  ]
  1 => array:3 [
    "query" => "select * from `orders` where `orders`.`id` = ? limit 1"
    "bindings" => array:1 [
      0 => 6219
    ]
    "time" => 0.0
  ]
]

Now this is confusing because runing the query select * fromorderswhereorders.id= 6219 limit 1 on DB directly results in 1 Entry, Query took 0.0005 seconds.

So what could I do to debug Why laravel wont find my DB Entry? Hereys the migration:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class ImportOldAdresses extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {

        Schema::table('orders', function (Blueprint $table) {
            $table->string('oldName');
            $table->string('oldAdress');
        });

        // Load data to be imported.
        $oldOrders = json_decode(File::get('storage/olddata.json'), TRUE);

        // Update.
        foreach ($oldOrders as $rawOrder) {
            $order = \App\Order::find(intval($rawOrder['id'])); // Here's the crash right with the first iteration.

            $order->oldName   = $rawOrder['oldName'];
            $order->oldAdress = $rawOrder['oldAdress'];

            $order->save();
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropColumn('oldName');
            $table->dropColumn('oldAdress');
        });
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire