Download the PHP package alphasoft-fr/data-model without Composer
On this page you can find all versions of the php package alphasoft-fr/data-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data-model
Model Class
Installation
Use Composer
Composer Require
Requirements
- PHP version 7.3
Usage
Creating a Model
To create a new model, extend the Model
class and implement the required abstract methods: getDefaultAttributes
and getDefaultColumnMapping
.
Hydrating Data
You can create a new model instance and hydrate it with data using the constructor or the hydrate
method.
Getting and Setting Attributes
You can access attributes using getter and setter methods. Attributes are automatically mapped to their corresponding columns based on the column mapping configuration.
Converting to Array
You can convert a model to an array using the toArray
method.
Using with PDO
Suppose we have a UserModel
class based on the Model
class, and we want to manage user data in a database.
Example: Inserting Data
Example: Updating Data
I hope these examples meet your requirements. If you have any further questions or need more assistance, feel free to ask!
Available Methods
The Model
class provides the following methods to manipulate object data:
hydrate(array $data)
: Hydrates the object with the provided data.toArray()
: Converts the object to an associative array.get(string $property)
: Retrieves the value of an object property.set(string $property, $value)
: Sets the value of an object property.toDb()
: Converts the object to an associative array ready for database insertion.
Type-Specific Retrieval
You can also retrieve attribute values with specific data types using dedicated methods. These methods provide type-checking and do not allow for default values when the property is not defined or if the value is of the wrong type.
-
getString
retrieves a string value. -
getInt
retrieves an integer value. -
getFloat
retrieves a floating-point value. -
getBool
retrieves a boolean value. -
getArray
retrieves an array. -
getInstanceOf
retrieves an instance of a specified class, or null if it exists and is an instance of the specified class. getDateTime
retrieves aDateTimeInterface
instance, optionally specifying a format for parsing.
Please note that these methods will throw exceptions if the property is not defined or if the value is of the wrong type. If you want to allow default values, you can use the previous examples with default values, but they will not throw exceptions in those cases.
Configuring Attributes and Columns
To configure the attributes and columns of your model, you need to implement the following abstract methods in your model class:
getDefaultAttributes()
: Defines the default attributes of the object.
This method should be implemented to return an associative array representing the default attributes of the object, including their default values.
For example:
getDefaultColumnMapping()
: Defines the mapping between object properties and database columns.
This method should be implemented to return an associative array that maps object properties to their corresponding database columns.
For example:
getPrimaryKeyColumn()
: Get the name of the primary key column for the model.
For example:
This method should be implemented by subclasses to return the name of the column that serves as the primary key for the model's corresponding database table.
Method getPrimaryKeyValue()
The getPrimaryKeyValue()
method is a utility function that allows you to retrieve the value of the primary key column for the model object. This method is particularly useful when you need to fetch the primary key value of the model object for operations such as updates or deletions in the database.
This method directly utilizes the get()
method to retrieve the value of the primary key column, based on the configured primary key column name.
Here's an example illustrating the usage of the getPrimaryKeyValue()
method within the context of a model class:
In this example, getPrimaryKeyValue()
method directly retrieves the value of the primary key column (in this case, the user's ID) from the model object using the get()
method. This is a convenient way to obtain the primary key for subsequent operations, such as updating or deleting data in the database.
As always, ensure that you adjust the values and column names to match your specific model and database configuration.
ModelFactory
The ModelFactory
class provides convenient methods to create instances of your model classes and collections from arrays of data. This can be particularly useful for scenarios where you need to transform raw data into fully hydrated model instances.
Usage
First, include the necessary namespace for the ModelFactory
class at the top of your file:
Creating a Single Model Instance
You can use the createModel
method of the ModelFactory
class to create a single instance of your model using an array of data:
Replace YourModelClass
with the actual class name of your model.
Creating a Collection of Model Instances
If you have an array of data representing multiple models, you can use the createCollection
method to create a collection of model instances:
Replace YourModelClass
with your actual model class name.
Example
Here's an example demonstrating how to use the ModelFactory
class to create model instances:
Remember to adjust the namespace and class names to match your actual project structure.
Note
Before using the ModelFactory
class, ensure that your model classes are set up to work seamlessly with it. The ModelFactory
assumes that your models extend the Model
class and are designed to be hydrated from arrays of data.
License
This package is open-sourced software licensed under the MIT License.