Model » Field aliases
Use field aliases to map a fields between your code and MongoDB.
Field aliases with Mondoc
Quite commonly, you may want to use a different name for a field in your model, but still want to store it with a different name in MongoDB. This is where Mondoc's field aliases come in.
Did you know?Field aliases can be combined with Mondoc encryption. This gives you the flexibility to work how you want, while giving you the tools, and power, to keep your data secure.
How to define field aliases
Field aliases are defined in your model using the $mondocFieldAliases property. This property is an associative array, where the key is the name of the field in the database, and the value is the name of the field in your model.
/** * @var string[] */ protected array $mondocFieldAliases = [ 'n' => 'name', 'dob' => 'dateOfBirth', 'ssn' => 'socialSecurityNumber', ];
Using field aliases in your models
Mondoc supports the addition of $mondocFieldAliases to at any level of model within Mondoc.
From the top level MondocAbstractModel to the core MondocAbstractSubModel, which is commonly used for nested models (but also provides the backbone for all Mondoc models).
A working example of field aliases
Introduce $mondocFieldAliases to your model
<?php class MyModel extends \District5\Mondoc\Db\Model\MondocAbstractModel { /** * @var string */ protected string $name; /** * @var DateTime */ protected DateTime $dateOfBirth; /** * @var string */ protected string $socialSecurityNumber; /** * @var string[] */ protected array $mondocFieldAliases = [ 'n' => 'name', 'dob' => 'dateOfBirth', 'ssn' => 'socialSecurityNumber', ]; public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; } public function getDateOfBirth(): DateTime { return $this->dateOfBirth; } public function setDateOfBirth(DateTime $dateOfBirth): void { $this->dateOfBirth = $dateOfBirth; } public function getSocialSecurityNumber(): string { return $this->socialSecurityNumber; } public function setSocialSecurityNumber(string $socialSecurityNumber): void { $this->socialSecurityNumber = $socialSecurityNumber; } }
Create your model instance
<?php $model = new MyModel(); $model->setName('Joe Bloggs'); $model->setDateOfBirth(DateTime::createFromFormat('Y-m-d', '1984-01-24')); $model->setSocialSecurityNumber('123-45-6789'); $model->save();
Querying and using your model
Your model instance is now ready to be used. The properties on your model are as originally intended. We have simply mapped the field names in the database to the field names in your model.
Note:When querying for a field, you must use the name of the field in the database. This is because Mondoc uses the field name in the database to query for the field, not the name of the field in your model.
<?php // retrieve the model from the database (or use the available model instance) $model = MyService::getOneByCriteria([ 'n' => [ '$eq' => 'Joe Bloggs' ], ]); var_dump($model->getObjectIdString()); // Outputs: string(24) " < an ObjectId string > " var_dump($model->getName()); // Outputs: string(10) "Joe Bloggs" var_dump($model->getDateOfBirth()->format('Y-m-d H:i:s')); //Outputs: string(19) "1984-01-24 00:00:00" var_dump($model->getSocialSecurityNumber()); // Outputs: string(11) "123-45-6789"
Recommended reading:
- description Field encryption » Encrypting fields, or nested models in your model
- description Model traits » learn how to use traits to enhance your model
- description Model » learn how to define your models
- description Field encryption + field aliases » Using field encryption alongside aliased field names