Mongo Models
The framework includes MongoModel for document-backed models.
Use SQL Model when you are working with relational tables.
Use MongoModel when you want declared-property models backed by Mongo-style collections instead of SQL tables.
Basic Model
use Myxa\Mongo\MongoModel;
final class UserDocument extends MongoModel
{
protected string $collection = 'users';
// Mongo uses _id by default.
protected string|int|null $_id = null;
protected string $email = '';
protected string $status = '';
}
The same strict declared-property idea still applies:
- document fields should be declared as real properties
- unknown attributes are rejected during normal writes
- guarded, hidden, internal, and cast attributes still apply
- built-in casts currently include
DateTime,DateTimeImmutable, andJson
Typical Usage
UserDocument::setManager($mongoManager);
$user = UserDocument::create([
'email' => 'john@example.com',
'status' => 'active',
]);
$found = UserDocument::find($user->getKey());
$found->status = 'inactive';
$found->save();
Differences From SQL Models
MongoModelis document-backed- it does not use the SQL query builder
- it does not provide SQL-style relations like
hasMany()orbelongsTo()
Connection support today:
MongoManagerresolves named Mongo connections- each
MongoConnectionresolves named collections - the built-in collection implementation currently shipped by the framework is
InMemoryMongoCollection - custom collection backends can be added by implementing
MongoCollectionInterface
So at the moment, Mongo support is best described as:
- a document-model layer with a connection/collection abstraction
- built-in in-memory support for tests and local experiments
- room for custom adapters when you want a real external Mongo backend
This project does not scaffold Mongo connections yet, so treat this as a framework capability you can wire in when your app needs document storage.
Further Reading
- Database
- Database Models and Queries
vendor/200mph/myxa-framework/src/Mongo/README.md