Code Monkey home page Code Monkey logo

Comments (7)

purekid avatar purekid commented on July 17, 2024

'_type' is designed for retrieving a inheritance model.

class Animal {
  public $collection = 'animal';
}

class Mammal extends Animal {}
class Dog extends Mammal {}

$dog = new Dog('name'=>'Lucky');
$dog->save();

When retrieving a model,if we found this model have a parent class,we add '_type' to query params.

Query ['_type'=>'Dog']

$dog = Dog::one();

Query ['_type'=>'Mammal'] , Of course Dog belongs to Mammal,so we must retrieve the right dog .

$mammal = Mammal::one();

So in the db,the document of dog have a '_type' like this: ['Animal','Mammal','Dog']
And this is why '_type' stored as a array() in db.

from mongodm.

purekid avatar purekid commented on July 17, 2024

And you says right , I also think the current method is not a very good solution . we must have some ways to optimize these code.

from mongodm.

jrschumacher avatar jrschumacher commented on July 17, 2024

So in short this enables the ability to fetch all documents related to the parent branch.

Functionality

Currently one can take Parent::find() to get all documents which is a child of Parent so considering the following model

class Person extends Model {
  public $collection = 'people';
}

class Faculty extends Person {}
class Tenyear extends Faculty {}
class Visiting extends Faculty {}

class Staff extends Person {}
class IT extends Staff {}
class Tenyear extends IT {}
class Financial extends Staff {}

class Student extends Person {
  public $collection = 'student';
}

one would expect

Person::one() // latest person in or extending Person (i.e. Faculty, Tenyear, Visiting, Staff, IT, etc...)
Faculty::one() // latest person in or extending Faculty
IT::one() // latest person in or extending IT
//....

Issues

  • What would one expect with Tenyear::one() I suspect it would fetch all with Tenyear and Faculty.
  • If the model has a custom collection like (i.e. Student) then Person::one() would not be able find students.
    • I would say the only way current method wouldn't work as well as solution 2. The only foreseeable solution would be 1.

Possible Solutions

  1. Detect Type based on PHP classes rather than Mongo query
    • How would a parent know it's children?
      • Load all models into memory and use foreach(get_declared_classes() as $c) if($c instanceof get_class()) x //...
    • Force convention and build an advanced query using the various children determined by convention
      • Naming convention
        • More restrictive on developer and possible difficult to integrate into existing applications
      • Namespace convention
        • Something like ParentNS/ParentCN
          • i.e. namespace Model/Person/Faculty/Tenyear
  2. Store type as hierarchical string using $regex

from mongodm.

jrschumacher avatar jrschumacher commented on July 17, 2024

@purekid do you have any more thoughts on this?

from mongodm.

purekid avatar purekid commented on July 17, 2024

@jrschumacher,i have some ideas but not clear.I will do some test this night at home ,then continue here.

from mongodm.

jrschumacher avatar jrschumacher commented on July 17, 2024

@purekid Any thoughts about this?

from mongodm.

jrschumacher avatar jrschumacher commented on July 17, 2024

Also need to consider indexing cost of this. I've been running dex and it seems this has a cost on collections.

from mongodm.

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.