Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
- [Cross-Database Relationships](#cross-database-relationships)
- [Authentication](#authentication)
- [Queues](#queues)
- [Debugging](#debugging)
- [->toSql()](#tosql)
- [Upgrading](#upgrading)
- [Upgrading from version 2 to 3](#upgrading-from-version-2-to-3)

Expand Down Expand Up @@ -731,7 +733,7 @@ The belongsToMany relation will not use a pivot "table" but will push id's to a
If you want to define custom keys for your relation, set it to `null`:

```php
use Jenssegers\Mongodb\Eloquent\Mode;
use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model
{
Expand Down Expand Up @@ -1075,6 +1077,24 @@ Last, add the service provider in `config/app.php`:
Jenssegers\Mongodb\MongodbQueueServiceProvider::class,
```

Debugging
---------

### ->toSql()

Hence we are in a NoSQL driver, it is counter-intuitive to use `toSql()` to output the string that will be executed.

However, if you still need the string that looks like the sql for some inspection, you can use the `toSql()` method:

```php
User::where('some_field', '>', 300)
->whereRaw(['age' => ['$gt' => 30, '$lt' => 40]])
->toSql();

// output
"select * from "users" where "some_field" > ? and {"age":{"$gt":30,"$lt":40}}"
```

Upgrading
---------

Expand Down
15 changes: 15 additions & 0 deletions src/Jenssegers/Mongodb/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,19 @@

class Grammar extends BaseGrammar
{
/**
* {@inheritdoc}
Copy link
Contributor

@Smolevich Smolevich Jan 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add param doc for argument $query and for return type please

*/
protected function compileWheresToArray($query)
{
return collect($query->wheres)->map(function ($where) use ($query) {
$baseWhere = $this->{"where{$where['type']}"}($query, $where);

if (isset($where['sql']) && is_array($where['sql'])) {
$baseWhere = json_encode($baseWhere);
}

return $where['boolean'].' '.$baseWhere;
})->all();
}
}
23 changes: 22 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public function testCarbonDateMockingWorks()

Carbon::setTestNow($fakeDate);
$item = Item::create(['name' => 'sword']);

$this->assertLessThan(1, $fakeDate->diffInSeconds($item->created_at));
}

Expand Down Expand Up @@ -572,4 +572,25 @@ public function testChunkById(): void

$this->assertEquals(3, $count);
}

public function testToSql(): void
{
$this->assertTrue(
is_string(User::where('age', 30)->toSql())
);

$this->assertTrue(
is_string(User::whereRaw(['age' => ['$gt' => 30, '$lt' => 40]])->toSql())
);

$this->assertEquals(
'select * from "users" where "some_field" > ?',
User::where('some_field', '>', 300)->toSql()
);

$this->assertEquals(
'select * from "users" where "some_field" > ? and {"age":{"$gt":30,"$lt":40}}',
User::where('some_field', '>', 300)->whereRaw(['age' => ['$gt' => 30, '$lt' => 40]])->toSql()
);
}
}