Code Monkey home page Code Monkey logo

Comments (5)

armanist avatar armanist commented on August 15, 2024

I can give more insights if required, I just need to know it's not supported or I'm doing something wrong?

from sleekdb.

Timu57 avatar Timu57 commented on August 15, 2024

Hi @armanist thank you for your question.
Sorry for the late response.

If you want to use select after a join it should be possible.
You can read more about that at https://sleekdb.github.io/#/execution-order

Could you please provide an example result without the select? Perhaps I can then help you with what is going wrong 😊

from sleekdb.

Timu57 avatar Timu57 commented on August 15, 2024

@armanist I think I know what the problem is:

$users = $userQueryBuilder
 ->join(function($user) use ($userBuiographyStore) {
    return $userBiographyStore->findBy(["user", "=", $user["_id"]]);
  }, "userBiographies")
  ->getQuery()
  ->fetch();
// $users should be something like this:
// {'_id' => 2, 'name' => 'Tom',  'userBiographies' => [{'age' => 16, ...}, {'age' => 18, ...}, ...], ... }

As you can see userBiographies is an array of documents.

Now you can try the following to select:

$users = $userQueryBuilder
  ->select(['age' => 'userBiographies.0.age'])
  ->join(function($user) use ($userBuiographyStore) {
    return $userBiographyStore->findBy(["user", "=", $user["_id"]]);
  }, "userBiographies")
  ->getQuery()
  ->fetch();
// or with a custom select function if the above does not work
$users = $userQueryBuilder
  ->select([
    'age' => function($user) {
      return $user['userBiography'][0]['age'];
    }
  ])
  ->join(function($user) use ($userBuiographyStore) {
    return $userBiographyStore->findBy(["user", "=", $user["_id"]]);
  }, "userBiographies")
  ->getQuery()
  ->fetch();

We are aware of the problem of handling documents within arrays. We plan to implement a wildcard like userBiographies.*.age or a built-in select function to get the first result, with the next major update. 😊

Instead of saving the userBiography in another "table" (store) and handle the situation the "relational"-way, you should think about saving the userBiography directly in the user document (the document/nosql-based-approach).

{
  "_id": 2,
  "name": "Tom",
  "userBiography": {
    "age": 18,
    "work": "some work"
  },
  "address": {
    "street": "some street",
    "postalCode": "0129479",
  }
}

Example SQL vs NoSQL

Hope I could help 😁

from sleekdb.

armanist avatar armanist commented on August 15, 2024

Hi @Timu57, thank you very much for great details, I will try your suggested way above ->select(['age' => 'userBiographies.0.age']) and will let you know here, the wildcard implementation will be great addition though ( userBiographies.* ). Meantime you can have look at SleekDB integration to the framework we are developing https://github.com/softberg/quantum-php-core/tree/master/src/Libraries/Database/Sleekdb.

from sleekdb.

Timu57 avatar Timu57 commented on August 15, 2024

I'm closing this issue for now. Feel free to reopen it if needed.

from sleekdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.