Comments (5)
@rakibtg Your right, for more complex joins that is excellent and a very nice solution.
I think if the developer just needs simple joins, providing an alternative method can make the business code much cleaner and the usage much easier.
$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$users = $userStore
->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
->fetch();
If the developer has to join three stores:
$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$commentStore = \SleekDB\SleekDB::store('comments', './sleekdb_test_scripts');
$users = $userStore
->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
->simpleJoin($commentStore, ['_id', '=', 'user'], 'comments')
->fetch();
If the developer needs for example also the articles related to the comments:
$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$commentStore = \SleekDB\SleekDB::store('comments', './sleekdb_test_scripts');
$users = $userStore
->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
->simpleJoin([$commentStore, $articleStore], [['_id', '=', 'user'], ['comments.article', '=', '_id']], 'comments')
->fetch();
Or:
$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$commentStore = \SleekDB\SleekDB::store('comments', './sleekdb_test_scripts');
$users = $userStore
->simpleJoin($articleStore, ['_id', '=', 'author'], 'articles')
->simpleJoin([$commentStore, $articleStore], [['_id', '=', 'user'], ['article', '=', '_id']], 'comments')
->fetch();
Where the second condition just consider the comments-result and joins it with the article store.
But I don't know what could be a nice name for the method.
from sleekdb.
Data output for above sample query:
Array
(
[0] => Array
(
[name] => Foo
[followers] => 10
[_id] => 1
[articles] => Array
(
[0] => Array
(
[title] => Lorem ipsum
[author] => 1
[likes] => 0
[_id] => 1
)
[1] => Array
(
[title] => Flock of birds
[author] => 1
[likes] => 66
[_id] => 3
)
[2] => Array
(
[title] => Flying in above
[author] => 1
[likes] => 0
[_id] => 4
)
)
)
[1] => Array
(
[name] => Bar
[followers] => 0
[_id] => 2
[articles] => Array
(
[0] => Array
(
[title] => Dollar sit amet
[author] => 2
[likes] => 12
[_id] => 2
)
)
)
[2] => Array
(
[name] => Bif
[followers] => 5
[_id] => 3
[articles] => Array
(
[0] => Array
(
[title] => The sun rise
[author] => 3
[likes] => 2842
[_id] => 5
)
)
)
[3] => Array
(
[name] => Baf
[followers] => 12
[_id] => 4
[articles] => Array
(
)
)
)
from sleekdb.
That is an awesome idea!
Wouldn't it be nicer if we could tweak the join method, for a much easier use?
I am thinking about something like this:
public function join($storeToJoin, $condition, $resultPropertyName)
With an usage like this:
$articleStore = \SleekDB\SleekDB::store('article', './sleekdb_test_scripts');
$userStore = \SleekDB\SleekDB::store('user', './sleekdb_test_scripts');
$users = $userStore->join($articleStore, ['author', '=', '_id'], 'articles')->fetch();
Or add a new method called leftJoin with a more easy use.
from sleekdb.
@Timu57 Thanks for your input. I have thought about it at first, but then I realised that we could achieve more with closure JOIN function.
For example,
- Nested join
- Nested join pagination
- Sorting ... etc.
Code example:
<?php
$userStore
->join(function ($user) use ($articleStore) {
return $articleStore
->where('author', '=', $user['_id'])
->join(function ($article) use ($userStore) {
return $userStore
->in('_id', '=', $article['likedUsers'])
->orderBy('desc', '_id' )
->limit(10);
}, 'likedBy');
}, 'articles')
->fetch();
Let me know your opinion.
from sleekdb.
Added in SleekDB 2.0!
from sleekdb.
Related Issues (20)
- Math & Logic operations HOT 2
- nested column select and distinct return empty array HOT 3
- simple join approach
- SleekDB Extension of builtin "named functions" by users
- Allow to use UUIDs as primary keys HOT 2
- How to Run SleekDB HOT 4
- JSON Wildcard Notation HOT 2
- Incorrect escaping of characters?
- Select with alias at join HOT 5
- Class 'SleekDB\Store' not found HOT 2
- how we do search with join HOT 6
- Searching for value in array. HOT 2
- [Feature Request] Simple key-value lookup HOT 1
- [Feature Request] Searching from multiple stores HOT 1
- [SUGGESTION] please set config:timeout default to false HOT 2
- How to set special chars? HOT 7
- Use SleekDB as a readonly store HOT 2
- updateById method undefined HOT 3
- Group by Date HOT 1
- replace file HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sleekdb.