flobauer's avatar

Models, DB for fast Processes

Hello everybody,

I just started with Lumen and i transfer a pure PHP written Skript into Lumen. The Skript should return its result very, very fast so I tried to transfer every task that can be done later to the queue.

One of this tasks is saving the data to a remote Database.

My questions:

  • When I initiate a new Eloquent Model, will it try to connect to the DB even before I do save()? (I talk about the models that have the remote db connection)
  • Can I somehow put the DB save process to the queue? When I give the Object to the Queue to save it (without an save() before), it will only serialize the id and will not be able to fetch it from the worker (because the model is not saved yet).

The only solution I see right now is to prepare the data for the model and create the eloquent model in the worker (which is not that nice).

Any Ideas are appreaciated, thank you

0 likes
4 replies
bobbybouwmann's avatar

I think you can do something like this

$model = new Model();
$model->name = $name;
$model->email = $email

And pass this object to the queue and save it their. However, you won't know what the next id generated by the database will be, keep that in mind

flobauer's avatar

Hey, thank you for your response!

I have a custom id, so that wouldn't be a problem. I tried your suggestion, my job looked like this:

    public function __construct(Myobject $myobject)
    {
        $this->myobject = $myobject;
    }
    public function handle()
    {
        $this->myobject->save();   
    }

even if I don't Inject the dependency it still realized it is a eloquent object:

    public function __construct($myobject)

It gives me the error

 [Illuminate\Database\Eloquent\ModelNotFoundException]
  No query results for model [App\Models\Myobject].

In the Queue I can see, that it contains the App\Models\Myobject namespace for both tries.

flobauer's avatar
flobauer
OP
Best Answer
Level 1

How I solved it for now: I only have some parameters in the Model, so I create an array in the right format for the model::create function, give it to the job and the handler runs Model::create().

Better solutions welcome.

boynet's avatar

I think sending array to the job its the best choise.. if you want speed why to initiad eloquent model with data and than send it to the job? let the job do the hard job

Please or to participate in this conversation.