Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Query documents based on array of strings? #40

Closed
AdriaandeJongh opened this issue Jan 5, 2019 · 5 comments
Closed

Query documents based on array of strings? #40

AdriaandeJongh opened this issue Jan 5, 2019 · 5 comments

Comments

@AdriaandeJongh
Copy link

AdriaandeJongh commented Jan 5, 2019

Hey there!

I can't figure out how to do the following. Say I have this:

$database = new \Filebase\Database();
$database->get('obj1')->save();
$database->get('obj2')->save();
$database->get('obj3')->save();
$database->get('obj4')->save();

$array = array ('obj1', 'obj3');

Is there a way I can query, order, and return the database, with the full documents? The following obviously won't work:

$resultDocuments = $database->query()->where('id','IN',$array)->results(false);

But how would I go about something like this?

Note that I am specifically looking for a way to return the full documents, because I need the id's.

@timothymarois
Copy link
Member

@AdriaandeJongh So you're looking to return based on document id which is the name of the document itself? One thing I would suggest is, add an id field inside your documents, this way the query method will search for that id field. And then it would return all the documents relating to that id (or 1) document if you're doing unique Ids.

@AdriaandeJongh
Copy link
Author

Even that wouldn't work because where doesn't take an array.

@timothymarois
Copy link
Member

@AdriaandeJongh I see. I'm swamped lately with work otherwise I'd look into adding that feature. If you think it's an easy add, run some unit tests and send a pull request, I'm sure just need to configure the query check using multiple on arrays. Its a feature we should have since it would work similar to a SQL IN clause.

@AdriaandeJongh
Copy link
Author

I spent an hour looking into this yesterday but having essentially no experience with PHP, coming from C#, I have absolutely no idea how the whole querying system works, so I'm going to leave this to you - sorry!

The workaround I just came up with that 'sort of' works for me right now :

$database = new \Filebase\Database();
$database->get('obj1')->save();
$database->get('obj2')->save();
$database->get('obj3')->save();
$database->get('obj4')->save();

$array = array ('obj1', 'obj3');

foreach($database as $entry)
{
	if(!in_array($entry->getId(), $array))
		continue;
	//etc
}

sumpygump pushed a commit to sumpygump/Filebase that referenced this issue Feb 8, 2019
@timothymarois
Copy link
Member

@AdriaandeJongh A recent update has provided you with a feature to accomplish this. Let me know if this resolves your issue. I will close it for now.

$documents = $db->where('__id', 'IN', ['id1', 'id2'])->results();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants