Skip to content

update fro, master fork to our build #3

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

Merged
merged 61 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
dfdcfc5
Make newPivotQuery function public
divine Mar 3, 2020
27acb90
Change laravel version to 6.x
divine Mar 3, 2020
279b18c
Merge pull request #1979 from divine/fix_make_pivot_public
Smolevich Mar 4, 2020
040c1fa
Make the truncate function only remove documents and not drop the col…
Yahatix Mar 10, 2020
cff95c3
Remove unnecessary dockblocks for collection::truncate
Yahatix Mar 11, 2020
a1777c9
Remove unnecessary use statement
Yahatix Mar 11, 2020
89667a5
Fix refresh() on EmbedsOne
stephandesouza Mar 11, 2020
a78d7ba
Removing unnecessary dockblocks
stephandesouza Mar 11, 2020
98bc2f5
Merge pull request #1996 from stephandesouza/fix-embedsone-refresh
Smolevich Mar 12, 2020
df682c1
Rename LICENSE to LICENSE.md
jenssegers Mar 26, 2020
8f10e33
Apply fixes from StyleCI
jenssegers Mar 26, 2020
2181c8a
Merge pull request #2003 from jenssegers/analysis-m41nJD
jenssegers Mar 26, 2020
a480b53
Update FUNDING.yml
jenssegers Mar 26, 2020
a7acac7
:memo: Add security information
jenssegers Mar 26, 2020
40f7264
Merge branch 'master' into master
divine Apr 10, 2020
336fd3a
query like on integer fields support
ilyasokay Apr 11, 2020
aab261b
common style code edit
ilyasokay Apr 15, 2020
4d8fb95
Merge pull request #2020 from ilyasokay/query-integer-fix
Smolevich Apr 15, 2020
84cd241
Add cursor support
Apr 20, 2020
bb776aa
Add cursor support
Apr 20, 2020
310f0a3
Add cursor support (tests)
Apr 20, 2020
0aff70a
Add cursor support (tests)
Apr 20, 2020
3d58b4b
Merge pull request #2024 from fsotomsk/master
Smolevich Apr 21, 2020
4f84a52
Merge branch 'master' into master
divine Apr 21, 2020
6408964
[GF] Added tests for negative dates
Apr 22, 2020
eeb6f26
Merge branch 'master' into add_test_for_negative_dates
Apr 22, 2020
f234065
Add test for millisecond-precision dates.
DFurnes Apr 22, 2020
90f73d2
Add failing test case for millisecond-precision dates before 1970.
DFurnes Apr 22, 2020
2f92654
Fix issue parsing millisecond-precision dates before 1970.
DFurnes Apr 22, 2020
736415e
Merge pull request #2028 from DoSomething/millisecond-date-bug
Smolevich Apr 23, 2020
44825e7
Merge branch 'master' into add_test_for_negative_dates
Apr 23, 2020
ac6b356
[GF] Added more tests for dates with different formats
Apr 27, 2020
e7bce2c
[GF] Fix style CI
Apr 27, 2020
1898947
Merge pull request #2027 from Giacomo92/add_test_for_negative_dates
Smolevich Apr 28, 2020
5cda0ce
Adds specific lumen queue config description
lissonpsantos2 May 1, 2020
8978432
Merge pull request #2032 from lissonpsantos2/master
Smolevich May 1, 2020
c615e9a
tests: for sorting
willtj May 26, 2020
4a7f5c1
Merge pull request #2039 from trybeapp/master
Smolevich May 29, 2020
74ce6c1
Update build-ci.yml
Smolevich Jun 2, 2020
ad50013
Using shivammathur/setup-php
Smolevich Jun 3, 2020
b1b27bc
Fix syntax error
Smolevich Jun 3, 2020
ec2bf87
Add name for step
Smolevich Jun 3, 2020
31ee326
Delete php 7.1 from matrix
Smolevich Jun 3, 2020
95551ca
Merge branch 'master' into master
Smolevich Jun 3, 2020
1375cdd
Merge pull request #2045 from jenssegers/fix-failed-command-in-ci
jenssegers Jun 7, 2020
7c6f5d6
Document MongoDB Authenticatable class usage
stephenjude Jul 10, 2020
fd3cfe2
fix typo
stephenjude Jul 10, 2020
79daae3
all good
stephenjude Jul 10, 2020
0bf3f03
Merge pull request #2063 from stephenjude/master
Smolevich Jul 10, 2020
032f3d0
Merge branch 'master' into master
divine Jul 18, 2020
f0f1eb4
always log queries
StevePorter92 Jul 22, 2020
baa7332
logging method no longer called
StevePorter92 Jul 22, 2020
08f4995
Merge pull request #2069 from StevePorter92/query-event-fix
divine Jul 22, 2020
2223548
Merge branch 'master' into master
divine Jul 22, 2020
828982f
Merge pull request #1993 from Yahatix/master
Smolevich Jul 22, 2020
31d004b
Use parent Builder's constructor. Ours was incomplete in that it did…
Aug 19, 2020
02d427b
Override Model::isGuardableColumn() to return true, since we don't re…
Aug 19, 2020
43b0789
Truncate Guarded collection in tests
Aug 19, 2020
dff4626
Correct guarded unit tests to be more relevant
Aug 20, 2020
2a8c2fd
Update readme to document guarding attributes
Aug 20, 2020
828e751
Merge pull request #2082 from fish3046/issue-2078
Smolevich Aug 20, 2020
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
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github: jenssegers
open_collective: laravel-mongodb
tidelift: "packagist/jenssegers/mongodb"
13 changes: 10 additions & 3 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ${{matrix.os}}
strategy:
matrix:
php: ['7.1', '7.2', '7.3', '7.4']
php: ['7.2', '7.3', '7.4']
os: ['ubuntu-latest']
mongodb: ['3.6', '4.0', '4.2']
services:
Expand All @@ -30,9 +30,16 @@ jobs:
name: PHP v${{ matrix.php }} with Mongo v${{ matrix.mongodb }}

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: "Installing php"
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl,mbstring,xdebug
coverage: xdebug
tools: composer
- name: Show PHP version
run: php${{ matrix.php }} -v && composer -V
run: php -v && composer -V
- name: Show Docker version
run: if [[ "$DEBUG" == "true" ]]; then docker version && env; fi
env:
Expand Down
File renamed without changes.
52 changes: 44 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
- [Extending the base model](#extending-the-base-model)
- [Soft Deletes](#soft-deletes)
- [Dates](#dates)
- [Guarding attributes](#guarding-attributes)
- [Basic Usage](#basic-usage)
- [MongoDB-specific operators](#mongodb-specific-operators)
- [MongoDB-specific Geo operations](#mongodb-specific-geo-operations)
Expand All @@ -42,6 +43,8 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
- [Cross-Database Relationships](#cross-database-relationships)
- [Authentication](#authentication)
- [Queues](#queues)
- [Laravel specific](#laravel-specific)
- [Lumen specific](#Lumen-specific)
- [Upgrading](#upgrading)
- [Upgrading from version 2 to 3](#upgrading-from-version-2-to-3)

Expand All @@ -63,7 +66,7 @@ Make sure you have the MongoDB PHP driver installed. You can find installation i
5.6.x | 3.4.x
5.7.x | 3.4.x
5.8.x | 3.5.x
6.0.x | 3.6.x
6.x | 3.6.x

Install the package via Composer:

Expand Down Expand Up @@ -230,6 +233,18 @@ class Book extends Model
}
```

### Extending the Authenticable base model
This package includes a MongoDB Authenticatable Eloquent class `Jenssegers\Mongodb\Auth\User` that you can use to replace the default Authenticatable class `Illuminate\Foundation\Auth\User` for your `User` model.

```php
use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{

}
```

### Soft Deletes

When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record.
Expand All @@ -249,6 +264,13 @@ class User extends Model

For more information check [Laravel Docs about Soft Deleting](http://laravel.com/docs/eloquent#soft-deleting).

### Guarding attributes

When choosing between guarding attributes or marking some as fillable, Taylor Otwell prefers the fillable route.
This is in light of [recent security issues described here](https://blog.laravel.com/security-release-laravel-61835-7240).

Keep in mind guarding still works, but you may experience unexpected behavior.

### Dates

Eloquent allows you to work with Carbon or DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database.
Expand Down Expand Up @@ -1067,6 +1089,8 @@ If you want to use MongoDB as your database backend, change the driver in `confi
'connections' => [
'database' => [
'driver' => 'mongodb',
// You can also specify your jobs specific database created on config/database.php
'connection' => 'mongodb-job',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
Expand All @@ -1078,23 +1102,31 @@ If you want to use MongoDB to handle failed jobs, change the database in `config

```php
'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database'),
'database' => env('DB_CONNECTION', 'mongodb'),
'driver' => 'mongodb',
// You can also specify your jobs specific database created on config/database.php
'database' => 'mongodb-job',
'table' => 'failed_jobs',
],
```

Or simply set your own `QUEUE_FAILED_DRIVER` environment variable to `mongodb`
```env
QUEUE_FAILED_DRIVER=mongodb
```
#### Laravel specific

Last, add the service provider in `config/app.php`:
Add the service provider in `config/app.php`:

```php
Jenssegers\Mongodb\MongodbQueueServiceProvider::class,
```

#### Lumen specific

With [Lumen](http://lumen.laravel.com), add the service provider in `bootstrap/app.php`. You must however ensure that you add the following **after** the `MongodbServiceProvider` registration.

```php
$app->make('queue');

$app->register(Jenssegers\Mongodb\MongodbQueueServiceProvider::class);
```

Upgrading
---------

Expand Down Expand Up @@ -1134,3 +1166,7 @@ Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rathe
```php
$books = $user->books()->sortBy('title')->get();
```

## Security contact information

To report a security vulnerability, follow [these steps](https://tidelift.com/security).
42 changes: 20 additions & 22 deletions src/Jenssegers/Mongodb/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,32 @@ public function __call($method, $parameters)
$start = microtime(true);
$result = call_user_func_array([$this->collection, $method], $parameters);

if ($this->connection->logging()) {
// Once we have run the query we will calculate the time that it took to run and
// then log the query, bindings, and execution time so we will report them on
// the event that the developer needs them. We'll log time in milliseconds.
$time = $this->connection->getElapsedTime($start);
// Once we have run the query we will calculate the time that it took to run and
// then log the query, bindings, and execution time so we will report them on
// the event that the developer needs them. We'll log time in milliseconds.
$time = $this->connection->getElapsedTime($start);

$query = [];
$query = [];

// Convert the query parameters to a json string.
array_walk_recursive($parameters, function (&$item, $key) {
if ($item instanceof ObjectID) {
$item = (string) $item;
}
});
// Convert the query parameters to a json string.
array_walk_recursive($parameters, function (&$item, $key) {
if ($item instanceof ObjectID) {
$item = (string) $item;
}
});

// Convert the query parameters to a json string.
foreach ($parameters as $parameter) {
try {
$query[] = json_encode($parameter);
} catch (Exception $e) {
$query[] = '{...}';
}
// Convert the query parameters to a json string.
foreach ($parameters as $parameter) {
try {
$query[] = json_encode($parameter);
} catch (Exception $e) {
$query[] = '{...}';
}
}

$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';
$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';

$this->connection->logQuery($queryString, [], $time);
}
$this->connection->logQuery($queryString, [], $time);

return $result;
}
Expand Down
19 changes: 18 additions & 1 deletion src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ protected function asDateTime($value)
{
// Convert UTCDateTime instances.
if ($value instanceof UTCDateTime) {
return Date::createFromTimestampMs($value->toDateTime()->format('Uv'));
$date = $value->toDateTime();

$seconds = $date->format('U');
$milliseconds = abs($date->format('v'));
$timestampMs = sprintf('%d%03d', $seconds, $milliseconds);

return Date::createFromTimestampMs($timestampMs);
}

return parent::asDateTime($value);
Expand Down Expand Up @@ -467,6 +473,17 @@ protected function getRelationsWithoutParent()
return $relations;
}

/**
* Checks if column exists on a table. As this is a document model, just return true. This also
* prevents calls to non-existent function Grammar::compileColumnListing()
* @param string $key
* @return bool
*/
protected function isGuardableColumn($key)
{
return true;
}

/**
* @inheritdoc
*/
Expand Down
57 changes: 45 additions & 12 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Connection;
use MongoCollection;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;
use RuntimeException;

/**
* Class Builder
* @package Jenssegers\Mongodb\Query
*/
class Builder extends BaseBuilder
{
/**
* The database collection.
* @var MongoCollection
* @var \MongoDB\Collection
*/
protected $collection;

Expand Down Expand Up @@ -209,12 +214,25 @@ public function get($columns = [])
return $this->getFresh($columns);
}

/**
* @inheritdoc
*/
public function cursor($columns = [])
{
$result = $this->getFresh($columns, true);
if ($result instanceof LazyCollection) {
return $result;
}
throw new RuntimeException("Query not compatible with cursor");
}

/**
* Execute the query as a fresh "select" statement.
* @param array $columns
* @return array|static[]|Collection
* @param bool $returnLazy
* @return array|static[]|Collection|LazyCollection
*/
public function getFresh($columns = [])
public function getFresh($columns = [], $returnLazy = false)
{
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
Expand Down Expand Up @@ -402,6 +420,14 @@ public function getFresh($columns = [])
// Execute query and get MongoCursor
$cursor = $this->collection->find($wheres, $options);

if ($returnLazy) {
return LazyCollection::make(function () use ($cursor) {
foreach ($cursor as $item) {
yield $item;
}
});
}

// Return results as an array with numeric keys
$results = iterator_to_array($cursor, false);
return $this->useCollections ? new Collection($results) : $results;
Expand Down Expand Up @@ -698,15 +724,11 @@ public function from($collection, $as = null)
/**
* @inheritdoc
*/
public function truncate()
public function truncate(): bool
{
$options = [
'typeMap' => ['root' => 'object', 'document' => 'object'],
];

$result = $this->collection->drop($options);
$result = $this->collection->deleteMany([]);

return (1 == (int) $result->ok);
return (1 === (int) $result->isAcknowledged());
}

/**
Expand Down Expand Up @@ -993,6 +1015,7 @@ protected function compileWhereAll(array $where)
protected function compileWhereBasic(array $where)
{
extract($where);
$is_numeric = false;

// Replace like or not like with a Regex instance.
if (in_array($operator, ['like', 'not like'])) {
Expand All @@ -1004,15 +1027,21 @@ protected function compileWhereBasic(array $where)

// Convert to regular expression.
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));
$plain_value = $value;

// Convert like to regular expression.
if (!Str::startsWith($value, '%')) {
$regex = '^' . $regex;
} else {
$plain_value = Str::replaceFirst('%', null, $plain_value);
}
if (!Str::endsWith($value, '%')) {
$regex .= '$';
} else {
$plain_value = Str::replaceLast('%', null, $plain_value);
}

$is_numeric = is_numeric($plain_value);
$value = new Regex($regex, 'i');
} // Manipulate regexp operations.
elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) {
Expand All @@ -1032,7 +1061,11 @@ protected function compileWhereBasic(array $where)
}

if (!isset($operator) || $operator == '=') {
$query = [$column => $value];
if ($is_numeric) {
$query = ['$where' => '/^'.$value->getPattern().'/.test(this.'.$column.')'];
} else {
$query = [$column => $value];
}
} elseif (array_key_exists($operator, $this->conversion)) {
$query = [$column => [$this->conversion[$operator] => $value]];
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected function buildDictionary(Collection $results)
/**
* @inheritdoc
*/
protected function newPivotQuery()
public function newPivotQuery()
{
return $this->newRelatedQuery();
}
Expand Down
Loading