roblesterjr04 / EloquentSalesForce

A SalesForce Package that works using the Laravel Eloquent Model structure.
MIT License
91 stars 62 forks source link

prepare() on null when massive update #102

Open Engveloper opened 1 year ago

Engveloper commented 1 year ago

Hello @roblesterjr04,

Thank you very much for your work in this package!.

I am reaching you because I am having an issue when I do a massive update in a query over a HasMany relationship.

$organization->affiliations()
            ->whereIn('Affiliated_Role__c', [Affiliation::APPROVER_ROLE, Affiliation::COORDINATOR_ROLE])
            ->whereNotIn('npe5__Contact__c', $newContactsIds)
            ->where(function ($query) {
                $query->whereNull('npe5__Status__c')
                    ->orWhere('npe5__Status__c', '<>', Affiliation::STATUS_FORMER);
            })->update([
                'npe5__EndDate__c' => now()->toDateString(),
                'npe5__Status__c' => Affiliation::STATUS_FORMER,
            ]);

When I run it, I am getting this exception

[2023-08-04 20:21:00] local.ERROR: Error: Call to a member function prepare() on null in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:566
Stack trace:
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(753): Illuminate\Database\Connection->Illuminate\Database\{closure}('update npe5__Af...', Array)

Debugging the query, in this case there isn't any record to update. Thanks in advance!.

roblesterjr04 commented 1 year ago

Hi, sorry for the late reply. Seeing as how the trace is reporting use of the Laravel connection class, i'd say the query is being executed against an eloquent model instead of the SOQL connection model. However, i can't confirm that without seeing more of how your models are set up. Part of me wonders if the closure query could be the issue. i'm surprised it hasn't come up yet if thats the case.

Engveloper commented 1 year ago

Hi @roblesterjr04, thank you for reply. Both models (Organization and Affiliation) extends from the Lester\EloquentSalesForce\Model class, and this is the relation definition inside the organization model:

public function affiliations(): SOQLHasMany
{
    return $this->hasMany(Affiliation::class, 'npe5__Organization__c', 'Id');
}