Coder Social home page Coder Social logo

Conversation with hundreds of participants cause error on mass insert: Prepared statement contains too many placeholders about chat HOT 11 CLOSED

silasrm avatar silasrm commented on August 16, 2024
Conversation with hundreds of participants cause error on mass insert: Prepared statement contains too many placeholders

from chat.

Comments (11)

silasrm avatar silasrm commented on August 16, 2024 2

A elegant solution is use configuration model class file on configuration on config/musonza_chat.php. With this, is easily to change any model. Like:

    'conversations_model_class' => \Musonza\Chat\Models\Conversation::class,
    'messages_model_class' => \Musonza\Chat\Models\Message::class,
    'message_notifications_model_class' => \Musonza\Chat\Models\MessageNotification::class,
    'participation_model_class' => \Musonza\Chat\Models\Participation::class,

I'll create this and make a PR.

from chat.

tolgatasci avatar tolgatasci commented on August 16, 2024

I did a research. maybe it will work for you. Who knows, you did the same.

foreach (array_chunk($data,1000) as $t)  
{
     DB::table('table_name')->insert($t); 
}

$i = 0;
 foreach ($conversation->participants as $participation) {
           $is_sender = ($message->participation_id == $participation->id) ? 1 : 0;

           $notification[] = [
               'messageable_id'   => $participation->messageable_id,
               'messageable_type' => $participation->messageable_type,
               'message_id'       => $message->id,
               'participation_id' => $participation->id,
               'conversation_id'  => $conversation->id,
               'is_seen'          => $is_sender,
               'is_sender'        => $is_sender,
               'created_at'       => $message->created_at,
           ];
  if($i>1000){
     self::insert($notification);
     $i = 0;
     $notification=[]
  }
$i++;
 }
self::insert($notification);

from chat.

silasrm avatar silasrm commented on August 16, 2024

I'm understand. But how you override this method?

from chat.

tolgatasci avatar tolgatasci commented on August 16, 2024

I'm understand. But how you override this method?

the code below didn't work?

I did a research. maybe it will work for you. Who knows, you did the same.

foreach (array_chunk($data,1000) as $t)  
{
     DB::table('table_name')->insert($t); 
}
$i = 0;
 foreach ($conversation->participants as $participation) {
           $is_sender = ($message->participation_id == $participation->id) ? 1 : 0;

           $notification[] = [
               'messageable_id'   => $participation->messageable_id,
               'messageable_type' => $participation->messageable_type,
               'message_id'       => $message->id,
               'participation_id' => $participation->id,
               'conversation_id'  => $conversation->id,
               'is_seen'          => $is_sender,
               'is_sender'        => $is_sender,
               'created_at'       => $message->created_at,
           ];
  if($i>1000){
     self::insert($notification);
     $i = 0;
     $notification=[]
  }
$i++;
 }
self::insert($notification);

from chat.

silasrm avatar silasrm commented on August 16, 2024

My doubt is about how override without change the original file (src/Models/MessageNotification.php).

from chat.

tolgatasci avatar tolgatasci commented on August 16, 2024

I am so sorry. I guess it's because this notification has fired up. It's like we can't manipulate it. The repo owner can help better.
@musonza

from chat.

silasrm avatar silasrm commented on August 16, 2024

I am so sorry. I guess it's because this notification has fired up. It's like we can't manipulate it. The repo owner can help better.
@musonza

Tks

from chat.

silasrm avatar silasrm commented on August 16, 2024

Good!

from chat.

musonza avatar musonza commented on August 16, 2024

@silasrm Yeah 20k is a lot. You can make a PR that adds a config option for batch insert and modify src/Models/MessageNotification.php to check for the configuration

from chat.

silasrm avatar silasrm commented on August 16, 2024

@silasrm Yeah 20k is a lot. You can make a PR that adds a config option for batch insert and modify src/Models/MessageNotification.php to check for the configuration

@tolgatasci fix, modifing the insert to insert 1k per time. When will a new version be released?

from chat.

silasrm avatar silasrm commented on August 16, 2024

My temporary solution, without modification of files package:

  1. Add entry in PSR-4 autoload on composer.json
    "autoload": {
        "psr-4": {
             ....
            "Musonza\\Chat\\Models\\": "app/Models/Chat/Overrider/"
        }
    },
  1. Execute composer dump-autoload
  2. Create a copy of original MessageNotification class and change to use Job to insert processing:
<?php

namespace Musonza\Chat\Models;

use App\Jobs\Chat\Message\CreateNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;
use Musonza\Chat\BaseModel;
use Musonza\Chat\ConfigurationManager;

class MessageNotification extends BaseModel
{
    use SoftDeletes;

    protected $table = ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE;
    protected $fillable = ['messageable_id', 'messageable_type', 'message_id', 'conversation_id'];
    protected $dates = ['deleted_at'];

    /**
     * Creates a new notification.
     *
     * @param Message      $message
     * @param Conversation $conversation
     */
    public static function make(Message $message, Conversation $conversation)
    {
        self::createCustomNotifications($message, $conversation);
    }

    public function unReadNotifications(Model $participant)
    {
        return self::where([
            ['messageable_id', '=', $participant->getKey()],
            ['messageable_type', '=', $participant->getMorphClass()],
            ['is_seen', '=', 0],
        ])->get();
    }

    public static function createCustomNotifications($message, $conversation)
    {
        Log::debug('aaaaaaaaaaaaaaaaaaaaaa');
        $notification = [];
        $i = 0;
        foreach ($conversation->participants as $participation) {
            $is_sender = ($message->participation_id == $participation->id) ? 1 : 0;

            $notification[] = [
                'messageable_id'   => $participation->messageable_id,
                'messageable_type' => $participation->messageable_type,
                'message_id'       => $message->id,
                'participation_id' => $participation->id,
                'conversation_id'  => $conversation->id,
                'is_seen'          => $is_sender,
                'is_sender'        => $is_sender,
                'created_at'       => $message->created_at,
            ];
            $i++;
            if ($i > 1000) {
                CreateNotification::dispatch($notification);
                $i = 0;
                $notification = [];
            }
        }
        CreateNotification::dispatch($notification);
    }

    public function markAsRead()
    {
        $this->is_seen = 1;
        $this->update(['is_seen' => 1]);
        $this->save();
    }
}
  1. Create the job class:
<?php

namespace App\Jobs\Chat\Message;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class CreateNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * CreateNotification constructor.
     * @param $data
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * @throws Exception
     */
    public function handle()
    {
        DB::table('chat_message_notifications')->insert($this->data);
    }
}

from chat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.