-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Sub Document - Array Collection Date and _id format. #1426
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
Comments
i have the same problem. |
Any updates on this? @jenssegers When selecting as single doc, dates displays as a date
But on select as related object I have this date conversion issue...
|
@janzell @SakyaVarro guys, have you found solution? |
Any update on this issue? |
Any update on this? |
same here issue return json data format issue any suggestion return format _id to string, and date format to string. thanks |
I've got the same issue. It seems that embedded Relationships are not getting serialized when calling the toArray() method on the parent model (which is called automatically by Laravel to prepare the data for the response). Therefore no mutations specified in the model class (e.g. $dates, $casts etc.) being executed and the _id value remains an instance of the MongoDB\BSON\ObjectId. To solve the problem i've extended the Jenssegers\Mongodb\Eloquent\Model class to be able to override the attributesToArray method: class MyModel extends Jenssegers\Mongodb\Eloquent\Model
{
/**
* Added serialization for embedded documents.
*
* @inheritDoc
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach($attributes as $key => $value) {
if($this->isEmbedsRelationship($key)) {
$attributes[$key] = $this->serializeEmbedded($key);
}
}
return $attributes;
}
/**
* Determine if the given key belongs to an embeds one or an embeds many relationship.
*
* @param string $key
*
* @return bool
*/
public function isEmbedsRelationship(string $key): bool
{
return method_exists($this, $key) && $this->getAttribute($key) instanceof EmbedsOneOrMany;
}
/**
* Serialize all embedded models to apply all mutations defined in the model class and
* to serialize the MongoId to string.
*
* @param string $key
*
* @return mixed
*/
protected function serializeEmbedded(string $key)
{
$data = $this->getAttribute($key);
$model = $this->$key()->getRelated();
if($data instanceof EmbedsMany) {
return $model::hydrate($data->toArray());
}
$attribute = $this->getAttribute($key);
return ((new $model)->newInstance($attribute, true))->toArray();
}
} To make this work you have to create a model class for each embedded document! class User extends MyModel
{
public function address()
{
return $this->embedsOne(Address::class);
}
public function addresses()
{
return $this->embedsMany(Address::class);
}
}
class Address extends MyModel
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at'];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = ['created_at' => 'datetime:d.m.Y'];
} When requesting a user all addresses are now serialized as expected. |
Hi guys, PRs are welcome, however keep in mind that support for embeds relationship will be dropped in the next version 4 release. The reason is simple, it doesn't work as people expect and has bugs which is nobody fixes. Version 3.6 will continue to receive fixes only. Thanks. |
Any updates on when we can expect version 4? Wondering whether to use embedded documents or not on a new project now. |
Hello @mattg66, You can use embedded documents without the "eloquent" magic that this library provides. Embedded documents are fine, however, the "magic" is what is causing issues. Regarding the new version don't expect it soon. Nobody likes breaking changes and it has been explained #1974 that there things that need to fixed upstream which is never going to happen - you might know the reason already. Thanks! |
I was able to work around this issue by formatting the dates at a resource level:
|
Hey guys,
I have a problem with the date and _id format for embedded documents which has an array of object collection values.
So for example, if I have a
Client
model which has anembedMany
relationship to an embedded document called employee (the employee is an array of objects embedded in a client model/document). See the example data below.{ "_id": "5a7a624bb9dfdc00a35c4669", "name": "jo3qmldbuglcsmdgkcoc", "label": "Beier Group", "enabled": false, "logo_image_id": "6d401890-a561-362a-b829-97cd3d20776e", "created_at": "1987-07-28 19:33:55", "updated_at": "2016-12-09 22:58:56", "product_id": "5a7a624bb9dfdc00a35c4665", "employees": [ { "user_id": 931, "name": "Gregory Stark", "profile_image_id": "20c07f90-1846-329c-9544-30c45407b4ef", "created_at": { "$date": { "$numberLong": "1490943600000" } }, "updated_at": { "$date": { "$numberLong": "1517969995000" } }, "_id": { "$oid": "5a7a624bb9dfdc00a35c4673" } }, { "user_id": 249, "name": "Mrs. Rhoda Orn", "profile_image_id": "b3e498de-ba91-325e-8e17-8258bdbb4362", "created_at": { "$date": { "$numberLong": "1490943600000" } }, "updated_at": { "$date": { "$numberLong": "1517969995000" } }, "_id": { "$oid": "5a7a624bb9dfdc00a35c4674" } } ] }
Now the problem is that the
_id
anddate
of the embedded document (employee) when getting the client collection was formatted asMongoDB\BSON\ObjectId
andobject(MongoDB\BSON\UTCDateTime)
respectively instead of astring
.This is only happening if the embedded document with an array of objects format. I'm guessing it is because of the
Jenssegers\Mongodb\Eloquent\Model.php
which only supports the first level of dot notation as you can see below. Does anyone having the same issue like this?The text was updated successfully, but these errors were encountered: