Skip to content

fix attribute vs relation priorty #2577

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

Open
wants to merge 27 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2e46a6d
use laravel get attribute
mshamaseen Aug 18, 2023
29935f2
update docker
mshamaseen Aug 19, 2023
a091681
update docker
mshamaseen Aug 20, 2023
07de6e5
Merge branch 'master' of https://github.com/mshamaseen/laravel-mongod…
mshamaseen Aug 20, 2023
9430d08
remove mongo db < 5 support
mshamaseen Aug 20, 2023
77dc9be
replace mongod with mongosh
mshamaseen Aug 20, 2023
80527f5
replace mongod with mongosh
mshamaseen Aug 20, 2023
e4fbb8d
replace mongod with mongosh
mshamaseen Aug 20, 2023
87b6453
replace mongod with mongosh
mshamaseen Aug 20, 2023
0a73a11
detach
mshamaseen Aug 20, 2023
5385aa4
remove mongo version 7 support as it is so new and yield to some errors
mshamaseen Aug 20, 2023
19db89b
Merge branch 'master' of github.com:mshamaseen/laravel-mongodb into n…
mshamaseen Aug 20, 2023
21fac7d
BelongsToMany relation now can't use the same relation name as column…
mshamaseen Aug 21, 2023
d0666b6
update documentation
mshamaseen Aug 21, 2023
dbfa900
Merge pull request #1 from mshamaseen/next-v4
mshamaseen Aug 21, 2023
61842f5
update readme
mshamaseen Aug 21, 2023
c8a6219
Merge branch 'master' of github.com:mshamaseen/laravel-mongodb
mshamaseen Aug 21, 2023
f18bf78
change package name for it to work with packagist
mshamaseen Aug 21, 2023
c0e1ee3
change package name for it to work with packagist
mshamaseen Aug 21, 2023
7a1fb98
add packagist using new package name
mshamaseen Aug 21, 2023
61a0505
Merge branch 'master' of github.com:jenssegers/laravel-mongodb into r…
mshamaseen Aug 22, 2023
09c1a9d
Merge branch 'master' of github.com:mshamaseen/laravel-mongodb into r…
mshamaseen Aug 22, 2023
815f37e
prepare for mering with upstream
mshamaseen Aug 22, 2023
21a46e1
return FUNDING.yml
mshamaseen Aug 22, 2023
cc1631e
fix php cs fixer
mshamaseen Aug 24, 2023
ed45cf7
revision updates
mshamaseen Aug 24, 2023
e3330ae
revert extra
mshamaseen Aug 24, 2023
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
Prev Previous commit
Next Next commit
Merge branch 'master' of github.com:jenssegers/laravel-mongodb into r…
…elease/v10

# Conflicts:
#	CHANGELOG.md
#	tests/Models/Group.php
#	tests/Models/MysqlGroup.php
#	tests/Models/MysqlUser.php
#	tests/Models/User.php
  • Loading branch information
mshamaseen committed Aug 22, 2023
commit 61a05057da9b50c56d04d3a9bf0d8506bb50218a
33 changes: 14 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
All notable changes to this project will be documented in this file.

## [Unreleased]
None yet.

## [10.0] - 2023-08-22

### Added
- MongoDB v6.0 supports and tests
- use Mongosh in tests instead of the old Mongo
- Mysql 8 Hybrid relation tests

### Removed
- MongoDB v4.* support was dropped

### Fixed
- Fixing the priority between attributes and relations dynamic property call to be the same as Laravel, that mean attribute first then relation (previous it was relation then attribute)
- using function names that exist as attribute accessor/caster: previously if you use a function name that has an accessor; say u use foo() and you have getFooAttribute, calling $model->foo will always fail as the function is called before the accessor, this has been fixed.

### Breaking
- EmbedsOne and EmbedsMany now require return type to work (Example in the documentation has been updated)
- You can't have the belongsToMany foreign key to be the same as the relation name (check documentation for more details)
- Add classes to cast `ObjectId` and `UUID` instances [#1](https://github.com/GromNaN/laravel-mongodb-private/pull/1) by [@alcaeus](https://github.com/alcaeus).
- Add `Query\Builder::toMql()` to simplify comprehensive query tests [#6](https://github.com/GromNaN/laravel-mongodb-private/pull/6) by [@GromNaN](https://github.com/GromNaN).
- Fix `Query\Builder::whereNot` to use MongoDB [`$not`](https://www.mongodb.com/docs/manual/reference/operator/query/not/) operator [#13](https://github.com/GromNaN/laravel-mongodb-private/pull/13) by [@GromNaN](https://github.com/GromNaN).
- Fix `Query\Builder::whereBetween` to accept `Carbon\Period` object [#10](https://github.com/GromNaN/laravel-mongodb-private/pull/10) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception for unsupported `Query\Builder` methods [#9](https://github.com/GromNaN/laravel-mongodb-private/pull/9) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception when `Query\Builder::orderBy()` is used with invalid direction [#7](https://github.com/GromNaN/laravel-mongodb-private/pull/7) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN).
- Remove public property `Query\Builder::$paginating` [#15](https://github.com/GromNaN/laravel-mongodb-private/pull/15) by [@GromNaN](https://github.com/GromNaN).
- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
- Accept operators prefixed by `$` in `Query\Builder::orWhere` [#20](https://github.com/GromNaN/laravel-mongodb-private/pull/20) by [@GromNaN](https://github.com/GromNaN).
- Remove `Query\Builder::whereAll($column, $values)`. Use `Query\Builder::where($column, 'all', $values)` instead. [#16](https://github.com/GromNaN/laravel-mongodb-private/pull/16) by [@GromNaN](https://github.com/GromNaN).
- Fix validation of unique values when the validated value is found as part of an existing value. [#21](https://github.com/GromNaN/laravel-mongodb-private/pull/21) by [@GromNaN](https://github.com/GromNaN).
- Support `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).

## [3.9.2] - 2022-09-01

### Addded
### Added
- Add single word name mutators [#2438](https://github.com/jenssegers/laravel-mongodb/pull/2438) by [@RosemaryOrchard](https://github.com/RosemaryOrchard) & [@mrneatly](https://github.com/mrneatly).

### Fixed
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ $users =
->get();
```

**NOT statements**

```php
$users = User::whereNot('age', '>', 18)->get();
```

**whereIn**

```php
Expand Down Expand Up @@ -477,6 +483,17 @@ Car::where('weight', 300)

### MongoDB-specific operators

In addition to the Laravel Eloquent operators, all available MongoDB query operators can be used with `where`:

```php
User::where($fieldName, $operator, $value)->get();
```

It generates the following MongoDB filter:
```ts
{ $fieldName: { $operator: $value } }
```

**Exists**

Matches documents that have the specified field.
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
],
"license": "MIT",
"require": {
"php": "^8.1",
"ext-mongodb": "^1.15",
"illuminate/support": "^10.0",
"illuminate/container": "^10.0",
"illuminate/database": "^10.0",
Expand All @@ -36,11 +38,9 @@
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/models",
"tests/seeds"
]
"psr-4": {
"Jenssegers\\Mongodb\\Tests\\": "tests/"
}
},
"suggest": {
"jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB",
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function getPayload($email, $token)
return [
'email' => $email,
'token' => $this->hasher->make($token),
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
'created_at' => new UTCDateTime(Date::now()),
];
}

Expand Down
3 changes: 3 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use MongoDB\BSON\ObjectID;
use MongoDB\Collection as MongoCollection;

/**
* @mixin MongoCollection
*/
class Collection
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use MongoDB\Database;
use Throwable;

/**
* @mixin Database
*/
class Connection extends BaseConnection
{
use ManagesTransactions;
Expand Down
63 changes: 63 additions & 0 deletions src/Eloquent/Casts/BinaryUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Jenssegers\Mongodb\Eloquent\Casts;

use function bin2hex;
use function hex2bin;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Jenssegers\Mongodb\Eloquent\Model;
use MongoDB\BSON\Binary;
use function str_replace;
use function substr;

class BinaryUuid implements CastsAttributes
{
/**
* Cast the given value.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if (! $value instanceof Binary || $value->getType() !== Binary::TYPE_UUID) {
return $value;
}

$base16Uuid = bin2hex($value->getData());

return sprintf(
'%s-%s-%s-%s-%s',
substr($base16Uuid, 0, 8),
substr($base16Uuid, 8, 4),
substr($base16Uuid, 12, 4),
substr($base16Uuid, 16, 4),
substr($base16Uuid, 20, 12),
);
}

/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return Binary
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof Binary) {
return $value;
}

if (is_string($value) && strlen($value) === 16) {
return new Binary($value, Binary::TYPE_UUID);
}

return new Binary(hex2bin(str_replace('-', '', $value)), Binary::TYPE_UUID);
}
}
46 changes: 46 additions & 0 deletions src/Eloquent/Casts/ObjectId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Jenssegers\Mongodb\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Jenssegers\Mongodb\Eloquent\Model;
use MongoDB\BSON\ObjectId as BSONObjectId;

class ObjectId implements CastsAttributes
{
/**
* Cast the given value.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if (! $value instanceof BSONObjectId) {
return $value;
}

return (string) $value;
}

/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof BSONObjectId) {
return $value;
}

return new BSONObjectId($value);
}
}
11 changes: 5 additions & 6 deletions src/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;

/**
* Embeds relations for MongoDB models.
*/
trait EmbedsRelations
{
/**
Expand All @@ -23,9 +26,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[, $caller] = debug_backtrace(false);

$relation = $caller['function'];
$relation = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

if ($localKey === null) {
Expand Down Expand Up @@ -58,9 +59,7 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[, $caller] = debug_backtrace(false);

$relation = $caller['function'];
$relation = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

if ($localKey === null) {
Expand Down
12 changes: 6 additions & 6 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use Jenssegers\Mongodb\Relations\MorphMany;
use Jenssegers\Mongodb\Relations\MorphTo;

/**
* Cross-database relationships between SQL and MongoDB.
* Use this trait in SQL models to define relationships with MongoDB models.
*/
trait HybridRelations
{
/**
Expand Down Expand Up @@ -134,9 +138,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[$current, $caller] = debug_backtrace(false, 2);

$relation = $caller['function'];
$relation = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

// Check if it is a relation with an original model.
Expand Down Expand Up @@ -178,9 +180,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if ($name === null) {
[$current, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$name = $caller['function'];
$name = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

[$type, $id] = $this->getMorphs(Str::snake($name), $type, $id);
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function getDateFormat()
*/
public function freshTimestamp()
{
return new UTCDateTime(Date::now()->format('Uv'));
return new UTCDateTime(Date::now());
}

/**
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.