Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero wallet balance when querying the wallet with Query Builder #993

Closed
ISNewton opened this issue Sep 11, 2024 · 8 comments
Closed

Zero wallet balance when querying the wallet with Query Builder #993

ISNewton opened this issue Sep 11, 2024 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@ISNewton
Copy link

Describe the bug
When using the Laravel Query Builder I get the balance as 0 , But when I switch to Laravel Eloquent ORM I get the correct balance 5000.

        $student = User::find(1);

        $wallet = $student->createWallet([
            'name' => 'Student wallet',
            'slug' => 'MyUniqueSlug',
            'meta' => ['is_active' => true]
        ]);

        $wallet->deposit(5000);

Now this query will return the correct wallet but with the balance 0:

        $students = DB::table('users')
            ->join('wallets', function ($join){
                $join->on('wallets.holder_id', 'users.id')
                    ->where('wallets.holder_type', User::class)
                    ->where('wallets.slug', 'MyUniqueSlug');
                ;
            })

But this query will return the correct wallet with the correct balance 5000

        $students = Student::query()
            ->withWhere('wallets', function ($query) {
                $query->where('wallets.slug' , 'MyUniqueSlug',);
            })
            ->get();

I can confirm that the transactions was created.

To Reproduce
Steps to reproduce the behavior:

  1. Create a user wallet
  2. Deposit 5000
  3. Query the wallet with both ORM and Query Builder
  4. You should see different balances.

Server:

  • php version: 8.3
  • database: 8.0.3
  • wallet version 10.1.6
@ISNewton ISNewton added bug Something isn't working question Further information is requested labels Sep 11, 2024
@ISNewton ISNewton changed the title Wallet balance is zero when querying the wallet when using Laravel Query Builder Zero wallet balance when querying the wallet with Query Builder Sep 11, 2024
@rez1dent3
Copy link
Member

Hello. I can't reproduce it, but I'll try again a little later.

Screenshot 2024-09-11 at 18 38 20
/** @var \App\Models\User $student */
$student = \App\Models\User::find(1);

$wallet = $student->createWallet([
    'name' => 'Student wallet',
    'slug' => 'MyUniqueSlug',
    'meta' => ['is_active' => true]
]);

$wallet->deposit(5000);

$students = DB::table('users')
    ->join('wallets', function ($join){
        $join->on('wallets.holder_id', 'users.id')
            ->where('wallets.holder_type', \App\Models\User::class)
            ->where('wallets.slug', 'MyUniqueSlug');
        ;
    });

dd($students->get());

@rez1dent3
Copy link
Member

Hmm. Do you have an open transaction? The balance is updated before committing to the database.

That is, you technically will not be able to get a new balance inside the transaction in the database, because it does not exist yet.

This is done as part of optimization:
#589
#562

@rez1dent3
Copy link
Member

Is this normal?

Screenshot 2024-09-11 at 22 06 37

@rez1dent3
Copy link
Member

I've been looking and debugging for a long time. I don't see a bug. At the moment I've removed the "bug" tag until a detailed case from your side.

@rez1dent3 rez1dent3 removed the bug Something isn't working label Sep 11, 2024
@ISNewton
Copy link
Author

I was running the code inside a PHPUnit test class that uses the DatabaseTruncation trait .
Thanks again @rez1dent3 .

@rez1dent3
Copy link
Member

This event makes a query to update the balance in the wallet table. If you do DB::commit() before your query, the data will be up-to-date.

// Check if the transaction level is 1 indicating the top level of the transaction
// The transaction level represents the nesting level of the transaction.
// The top level of the transaction is 1, indicating that the current transaction is the outermost transaction.
if ($connection->transactionLevel() === 1) {
// Call the `committing` method of the `RegulatorServiceInterface`
// This method is responsible for performing actions when a transaction is successfully committed.
// It is typically used to update the transaction status in the database.
// The `committing` method is called to perform actions like updating the transaction status in the database.
app(RegulatorServiceInterface::class)->committing();
}

@rez1dent3
Copy link
Member

Inside the committing method:

$this->walletRepository->updateBalances($balances);

@rez1dent3
Copy link
Member

In general, the functionality appeared in the framework only for the sake of our project.

https://github.com/laravel/framework/pull/44608/files

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants