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

Reference ids in subdocuments saved as string #256

Open
rrr-bot opened this issue May 16, 2018 · 4 comments
Open

Reference ids in subdocuments saved as string #256

rrr-bot opened this issue May 16, 2018 · 4 comments
Labels
status:to be verified Needs to be reproduced and validated.

Comments

@rrr-bot
Copy link

rrr-bot commented May 16, 2018

What steps will reproduce the problem?

$model = $this->findModel($intent);
$entity = $this->findOtherModel($id);
$model->parameters = [
[
'entity' => $entity->_id,
'value' => '1'
],
[
'entity' => $entity->_id,
'value' => '2'
],
];
$model->save();

What's expected?

ObjectId

What do you get instead?

String

Additional info

it generates BulkWrite query to MongoDB. All of 'enitity' attributes in array of subdocuments are casted to String 'MongoDB\BSON\ObjectID(abcabcabcabcabcabcabc)'. Is anyone know why and how to fi this?

Q A
Yii version
Yii MongoDB version
MongoDB server version
PHP version
Operating system
@hoang-rio
Copy link

I think you need read MongoId specifics
You must cast _id to string

@samdark samdark closed this as completed Jun 20, 2018
@rrr-bot
Copy link
Author

rrr-bot commented Jun 20, 2018

You didn't get it. It is not string 'abcabcabcabcabcabcabc' which can be cast to ObjectId but string: 'MongoDB\BSON\ObjectID(abcabcabcabcabcabcabc)' which cannot! If I leave 'abcabcabcabcabcabcabc' I will have string in DB not the proper ObjectId. There is now way to save ObjectId as a document property which is an array type. Why it is cast to a string with class name?

@samdark samdark reopened this Jun 20, 2018
@hoang-rio
Copy link

You must cast it before set to entity when it is ObjectID:
I think code like

$model = $this->findModel($intent);
$entity = $this->findOtherModel($id);
$model->parameters = [
[
'entity' => (string) $entity->_id,
'value' => '1'
],
[
'entity' => (string) $entity->_id,
'value' => '2'
],
];
$model->save();

may work

@rrr-bot
Copy link
Author

rrr-bot commented Jun 22, 2018

Nope, you get string, not the ObjectId. The question is why it is cast to weird string - 'MongoDB\BSON\ObjectID(abcabcabcabcabcabcabc)' rather than leave as ordinary ObjectId?
If you have similar code like this:

$model = $this->findModel($intent);
$entity = $this->findOtherModel($id);
$model->parameters = $entity->_id;
$model->save();

it works (leave ObjectId).

@bizley bizley added the status:to be verified Needs to be reproduced and validated. label Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated.
Projects
None yet
Development

No branches or pull requests

4 participants