Comments (7)
'_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.
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.
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
) thenPerson::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
- 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 //...
- Load all models into memory and use
- 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
- i.e.
- Something like ParentNS/ParentCN
- Naming convention
- How would a parent know it's children?
- Store type as hierarchical string using $regex
- $regex is slow
- Using
/^a.*$/
is efficiently optimized- i.e.
/^\/Person\/Faculty\/Tenyear.*
- i.e.
- Using
- $regex is slow
from mongodm.
@purekid do you have any more thoughts on this?
from mongodm.
@jrschumacher,i have some ideas but not clear.I will do some test this night at home ,then continue here.
from mongodm.
@purekid Any thoughts about this?
from mongodm.
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)
- Embedding: where's the id of the embedded object? HOT 4
- unsupported for Latest extension 'MongoDB'
- Remove _id dependancy of MongoId
- Model (with Reference to Json) HOT 3
- Edit and object HOT 2
- Add Late Static Binding and fix some methods call (i.e. static instead of self) HOT 1
- Geolocation data handling
- Inconsistent behavior when Mongo is not running HOT 3
- Mongodm can not be use in Phalcon? HOT 5
- mongo transaction HOT 1
- Method conflicts with magic methods
- slow to update HOT 1
- is this Compatibled with mongodb 3.0+? HOT 2
- any examples with gridfs?
- PHP 7 support? HOT 2
- MongoDB extension?
- Regex query is not working. HOT 1
- Error in example
- Unit tests support
- MongoDM not fully support PHP 7.1/PHP 7.2/PHP7.0 for find() function
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 mongodm.