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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package data-model

Model Class

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Installation

Use Composer

Composer Require

Requirements

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:

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.

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:

This method should be implemented to return an associative array representing the default attributes of the object, including their default values.

For example:

This method should be implemented to return an associative array that maps object properties to their corresponding database columns.

For example:

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.


All versions of data-model with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package alphasoft-fr/data-model contains the following files

Loading the files please wait ....