John Davidson

php - Serialization of 'PDO' is not allowed in a Job using Laravel 8

0 comments
Message:


I'm trying to automate a process composed from multiple jobs. Every job had a goal, so I'm running a global job who is responsible for creating child jobs and add them to a batch instance.


# Instantiate the batches array
$batches = [];
# Retrieve destinations that have a dataset
$destinations = Destinations::where('dataset','like','%xx%')
->where('dataset','like','%xx%')
->get();
# Checking if there is some results
if(!empty($destinations)){
foreach($destinations as $destination){
# Current DateTime
$dateTime = Carbon::now()->format('Y_m_d_h_i_s');
# Create an array of batches for every location
$batches[] = new AutoImport("auto_job_{$destination->reference}_{$dateTime}",$destination);
}
}

# Creating batch chainning
$batch = Bus::batch($batches)
->then(function (Batch $batch) {

})->catch(function (Batch $batch, Throwable $e) {

})->finally(function (Batch $batch) {

})->dispatch();

return $batch->id;

At some point I'm gettin this error "Serialization of 'PDO' is not allowed" and I think is coming from this line new AutoImport("auto_job_{$destination->reference}_{$dateTime}",$destination); The fact that I send a whole object into the batches array


I'm not sure of that, so any help will be welcome.


Job abstract class


<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Imtigger\LaravelJobStatus\Trackable;

abstract class Job implements ShouldQueue, ShouldBeUnique
{
/*
|--------------------------------------------------------------------------
| Queueable Jobs
|--------------------------------------------------------------------------
|
| This job base class provides a central location to place any logic that
| is shared across all of your jobs. The trait included with the class
| provides access to the "queueOn" and "delay" queue helper methods.
|
*/

use Batchable,InteractsWithQueue, Queueable, SerializesModels, Trackable;
}


My Answer:
data is the process of converting a data structure or object into a format that can be easily stored or transmitted. In PHP, serialization is typically done using the `serialize()` function, which converts the data into a string that can be stored in a file or database.

For example, if you have an array of data that you want to serialize, you can do so like this:

php
$data = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$serializedData = serialize($data);

// Store the serialized data in a file
file_put_contents('data.txt', $serializedData);


To unserialize the data and convert it back into its original form, you can use the `unserialize()` function:

php
$serializedData = file_get_contents('data.txt');
$data = unserialize($serializedData);

print_r($data);


This will output:


Array
(
[name] => John
[age] => 30
[city] => New York
)


Serialization is commonly used in PHP for storing complex data structures, caching data, or transmitting data between different systems. It is a convenient way to convert data into a format that can be easily manipulated and transported.

Rate this post

3 of 5 based on 8641 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us