kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
1.05k stars 169 forks source link

How to send datatype for a field (For example datatype as "reference") #17

Closed prajwalpoojari9876 closed 4 years ago

prajwalpoojari9876 commented 4 years ago

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.

jeromegamez commented 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.

prajwalpoojari9876 commented 4 years ago
$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

prajwalpoojari9876 commented 4 years ago

Hi @jeromegamez, did you get my question?

jeromegamez commented 4 years ago

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!

prajwalpoojari9876 commented 4 years ago

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.

Screenshot_66

jeromegamez commented 4 years ago

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.

prajwalpoojari9876 commented 4 years ago

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.

jeromegamez commented 4 years ago

🎉Glad I could help! Would it be okay for you if we close this issue?

prajwalpoojari9876 commented 4 years ago

Yes I'm closing this. Thanks for the help.