Closed prajwalpoojari9876 closed 4 years ago
What component are you referring to? Please provide a code example so that I can understand what you mean and replicate the behaviour.
$child = app('firebase.firestore')->database()->collection('Child')->newDocument();
$child->set([
'name' => 'John',
]);
$parent = app('firebase.firestore')->database()->collection('Parent')->newDocument();
$parent->set([
'Child' => \Google\Cloud\Firestore\FieldValue::arrayUnion([$child->path()])
]);
OR
$parent = app('firebase.firestore')->database()->collection('Parent')->newDocument();
$parent->set([
'Child' => $child->path()
]);
I want to set reference as datatype. Not string
Hi @jeromegamez, did you get my question?
Sorry for the late reply… unfortunately, I'm not super familiar with the Firestore component (this SDK just provides an entrypoint to google/cloud-firestore, but as far as I know it's not possible for a document to have a child - only collections can have children. You can see this in https://github.com/googleapis/google-cloud-php/blob/master/Firestore/src/DocumentReference.php#L70
So, if you wanted to have a reference between two documents, then using path strings and parsing them in your application would be the way to go.
I might be wrong - to be sure, I'd suggest asking the question again on StackOverflow or, if you think that this should be possible, as a feature request on https://github.com/googleapis/google-cloud-php/issues
I hope this helps!
ok @jeromegamez , thanks for all input you gave. it helps us in one or another way.
Now just to clear what we want to is send datatype of field. As you can see in screenshot, there are many datatypes of a field. like from string, boolean etc. There is one called "reference". We want to have this datatype. As we send path, it always considered as string.
I just dug a little bit through the source code... could you try this?
$child = app('firebase.firestore')->database()->collection('Child')->newDocument();
$parent = app('firebase.firestore')->database()->collection('Parent')->newDocument();
$parent->set([
'Child' => $child
]);
It looks like the Firestore library could be capable of resolving this correctly, see https://github.com/googleapis/google-cloud-php/blob/master/Firestore/src/ValueMapper.php#L310.
I don't know if the child document has to be saved beforehand though.
Thanks a lot @jeromegamez . It worked.
And I have checked the case the child document set method is to be called beforehand. Else it stores reference with child id but that child is not created.
🎉Glad I could help! Would it be okay for you if we close this issue?
Yes I'm closing this. Thanks for the help.
Whenever you send "Fieldname" => "value". It will take as string
So if we want to send "Fieldname" => "reference to document" Then also, it takes as string
Further, we want to datatype inside the array. ex: "Fieldname" => array('reference to document")
Please help.