Linked list for firestore documents - firebase

I have a use case to make a linked list of documents like
{ name, next_ptr} and next_ptr is a reference to another document
I have following code and I am getting next_ptr but not getting fields belonging to next_ptr
I am getting following output
KkkGTTKjuwcGmzJzQ3Wa => FIRE => undefined
iW9lm7sYkgvuZPdVvrZE => GAS => undefined
NtTyJNjqIT79PZ6zkqtY => WATER => undefined
Expected Output
KkkGTTKjuwcGmzJzQ3Wa => FIRE => GAS
iW9lm7sYkgvuZPdVvrZE => GAS => WATER
NtTyJNjqIT79PZ6zkqtY => WATER => undefined
Code
db = defaultApp.firestore() ;
abc_collection = db.collection("abc") ;
abc_collection.get()
.then( data => {
data.forEach(item => {
console.log(item.id , "=>", item.get('name')) ;
next_ref = item.get('next_ptr') ;
next_ref.get("name").then(item => {
console.log(item) ;
}).catch("") ;
} ) ;
}).catch("") ;

It looks like you're assuming that the query results contain all the referenced documents:
item.get('next_ptr')['name']
What item.get('next_ptr') returns is a DocumentReference object, not the entire contents of the document. You will have to query that document with its get() method in order to load its contents, or find the document using its ID in the results that you queried (if you guarantee that the reference always points to a document in the same collection.
You might just be better off storing the string ID of the document if you don't want to deal with the DocumentReference.

Related

CakePHP Collection Filters Return Different Object Type?

I'm running into unexpected behavior in CakePHP 3.x when using collection filters. When I provide a collection of type Cake\Collection\Collection, the filter returns a new collection with type Cake\Collection\Iterator\FilterIterator.
Here is some sample code that demonstrates the behavior:
$people = [
['name' => 'Alice', 'age' => 17],
['name' => 'Bob', 'age' => 51]
];
$collection = new Collection($people);
$adults = $collection->filter(function ($person, $key) {
return $person['age'] > 17;
});
The output of debuging $collection and $adults shows the type change:
object(Cake\Collection\Collection) {
'count' => (int) 2
}
object(Cake\Collection\Iterator\FilterIterator) {
'count' => (int) 1
}
This object type change is causing unexpected problems in my code. I've resolved the issue by rebuilding the collection after each filter. For example:
$adults = new Collection($adults->toArray());
This seems a little ugly. Is there a better method for retaining the Cake\Collection\Collection type? Any advice appreciated!

NHibernate QueryOver - Doing Fetches and OrderBy -> What Syntax to use?

All,
I have a query as such:
_AddOrderBy(sortOptions, query)
.Fetch(x => x.ImageType).Eager
.Fetch(x => x.User).Eager
.Fetch(x => x.Partner).Eager
.Inner.JoinAlias(x => x.Partner, () => p).Fetch(x => x.Company).Eager
.Skip(startIndex)
.Take(pageSize)
.List<ImageRequest>();
In the above QueryOver I call _AddOrderBy() method which adds an order by clause. The challenge I face is how do I create an "order by" that references a property (ordering by "CompanyName") that lies within the following association path without conflicting with my Fetch()/Inner joins:
ImageRequest.Partner.Company.CompanyName
Inside my _AddOrderBy() I have this:
Partner p = null;
Company comp = null;
order = query.Inner.JoinAlias(x => x.Partner, () => p)
.Inner.JoinAlias(x => x.Company, () => comp)
.OrderBy(x => comp.CompanyName);
But this gives me a run time exception stating that I have duplicate key (referring to Partner) in the criteria. I can see that this is conflicting with my eager fetching.
My questions is:
How do I add an "order by" so it works with my Fetching.
The beauty of using an Alias in QueryOver is that you don't have to use Fetch or JoinAlias in your _AddOrderBy() method again if the Join happens in the query already. You only need to declare the Alias with the same name.
Therefore your _AddOrderBy() can just look like this:
Partner p = null;
Company comp = null;
order = query
.Inner.JoinAlias(x => p.Company, () => comp) // you can use p here if it was used in the query before
.OrderBy(x => comp.CompanyName);
The reason this works is this: If you put the whole code into one method it will obviously work. Splitting it into two methods still works, because Partner p is not a reference to an object in memory but an Alias that is simply translated into a string for the SQL query.

How can I obtain the values of an Entity in Laravel-Backpack?

While I was trying to show pictures I made this pleasant discovery in backpack:
$this->crud->addFields([
[ // Upload
'name' => 'pictures', //<-- this is an Entity
'label' => 'Photos',
'type' => 'upload_multiple',
'upload' => true,
'disk' => 'uploads'
]
]);
This fragment of code gives me this:
My questions is:
What am I doing wrong?
How can get the value 'file' of this two vectors? I need this for display the images.
You probably forgot to add 'photos' to the cast array.
Based on the documentation:
Step 3. Since the filenames are stored in the database as a JSON array, we're going to use attribute casting on your model, so every time we get the filenames array from the database it's converted from a JSON array to a PHP array:
protected $casts = [
'photos' => 'array'
];
https://laravel-backpack.readme.io/docs/crud-fields#upload_multiple

Alter views search api solr result

I am using search api and Solr, When I echo the result variable , it gave me the following results
stdClass Object
(
[entity] => 442415
[_entity_properties] => Array
(
[search_api_relevance] => 1
[search_api_excerpt] =>
[search_api_item_id] => 442415
)
)
In views i added ( custom text field, that is global variable), With this nid 442415, i will do certain node load operation and finally,
i will get price for the specific product. This code block will execute inside the foreach.
_views_pre_render
Please guideme,
which hook i should use , _views_post_execute or _views_pre_render ?
How to assign new value in to which variable and how to print that in tpl
Finally, i want to display the price on each item
This link resolved my problem.
Solution approach
function mymodule_views_pre_render(&$view) {
if($view->name == 'my_view') {
foreach($view->result as &$row) {
$row->_entity_properties['nothing'] = 'new value';
}
}
}
views-view-field--view-name--display-name--nothing.tpl.php
print $row->_entity_properties['nothing'];

FQL: query for event table returns venue.name instead of venue.id

When making a query to get event details I seem to get venue.name instead of venue.id in the result set. Has there been an unannounced change in the table structure or am I doing something wrong. The Graph API Explorer gives the venue.id yet when using FQL through PHP SDK in my own web site it's the venue.name I get.
Heres the code:
$fql='{
"event_info": "SELECT name,description, pic_small,pic_big, eid,venue,location FROM event WHERE eid ='.$_GET['id'].'",
"event_venue":"SELECT name, username, page_id, location FROM page WHERE name IN (SELECT venue.id FROM #event_info)"
}';
$setup = array(
'method' => 'fql.multiquery',
'queries' => $fql,
'callback' => ''
);
$result = $facebook->api($setup);
This leads to the "event_venue" result set to be empty.
Here's the dump:
Array
(
[0] => Array
(
[name] => event_info
[fql_result_set] => Array
(
[0] => Array
(
[eid] => 410351692336116
[venue] => Array
(
[name] => Boothill
)
[location] => Boothill
)
)
)
[1] => Array
(
[name] => event_venue
[fql_result_set] => Array
(
)
)
)
If I test this query
SELECT name,description, pic_small,pic_big, eid,venue,location
FROM event WHERE eid ='410351692336116'
using the FQL tab (!) in the Graph API explorer, I get
"venue": {
"id": 126049334121592
}
and not the venue’s name.
And doing a multiquery like your’s with the second query being
"event_venue":"SELECT name, username, page_id, location FROM page
WHERE page_id IN (SELECT venue.id FROM #event_info)"
I’m getting the venue info as well.
Could you please check if you get a different result if you do your queries not using your $setup array with
'method' => 'fql.multiquery',
but just
$facebook->api('/fql?q='.urlencode('{ "event_info": "…", "event_venue": "… FROM page
WHERE page_id IN (SELECT venue.id FROM #event_info)" }'));
instead?
I've run into this issue today and spent quite some time troubleshooting it. It seems to be related to the Access Token. When I use my App's Access Token to request the venue data, for some venues all I I get is the venue.name field. However, if I use the Graph API Explorer to generate a different token, I get the venue.id field as expected.
I went as far as to replace the Graph API Explorer's generated Access Token with my App Token, and sure enough all I received back was venue.name.

Resources