Download the PHP package sanovskiy/simple-object without Composer

On this page you can find all versions of the php package sanovskiy/simple-object. 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 simple-object

SimpleObject 7.x Library Documentation

Introduction

SimpleObject is a PHP library designed to simplify working with database records by providing an intuitive and easy-to-use interface.

Installation

You can install SimpleObject via Composer:

Database Connection Configuration

Before you can start using SimpleObject, you need to configure connections to your database. This involves specifying the database driver, host, user credentials, database name, and other relevant settings. SimpleObject supports MySQL, PostgreSQL, and MSSQL database drivers.

Adding a Connection

You can add a database connection using the ConnectionManager class along with a ConnectionConfig object. Below is an example of how to add a MySQL database connection:

Explanation of Configuration Parameters

Subfolder Rules Configuration

The sub_folders_rules configuration option allows you to organize generated models into subfolders based on table prefixes. This can be useful for structuring your models in a more logical and organized manner, especially in projects with a large number of database tables.

Adding Subfolder Rules

You can specify subfolder rules using an associative array where the keys are table prefixes and the values are arrays containing the folder name and a flag indicating whether to strip the prefix from the model class names.

Example:

In this example:

Usage Notes

Models Generation

After adding connections and whenever there are changes in the database structure, or during the initial setup of your project, you need to call the reverseEngineerModels() method of the ModelsGenerator class to generate models based on existing database tables.

Ignoring Auto-generated Files

Since the Base folder contains auto-generated code, it's recommended to add it to your project's .gitignore file to avoid versioning generated files unnecessarily.

Example

Suppose you have tables named acl_user, acl_role, shop_product, and shop_order. Using the subfolder rules from the example above would result in the following folder structure:

This organization helps maintain a clear and organized structure within your project's models directory.

This service method is typically executed during project deployment or after modifications to the database structure. If your code is running in normal operation and the models have already been generated, there's no need to call this method.

This will create models corresponding to your database tables, making them ready for use with SimpleObject.

Model properties naming and table primary keys

All models and model properties are named in CamelCase by splitting table name or column by underscores. I.e: field model_id become property ModelId. Column SomeLongFieldName become Somelongfieldname. Limitation: Table PK should be named id to ensure full compatibility with SimpleObject.

Usage

SimpleObject supports basic CRUD operations (Create, Read, Update, Delete) for interacting with database records.

SimpleObject allows you to interact with database records using object-oriented models. Here's a basic example of how to use SimpleObject:

Model names and their properties precisely mirror the names of corresponding database objects but are converted to CamelCase. However, accessing properties by their corresponding database field names is permissible. Although this won't result in an error, these properties are not explicitly defined in the base class as @property and hence may not be recognized by IDEs.

Filter Class Documentation

The Filter class within the SimpleObject library is responsible for constructing SQL queries based on specified conditions. It allows users to filter database records effectively. Below is a detailed explanation of its functionality and the format of the filter array used to construct queries.

Filter Array Format

The filter array passed to the constructor follows a specific format to define conditions for filtering records. Each element of the array corresponds to a filter condition.

The top-level of the filter list is always combined using 'AND' logic.

Example Usage

You typically do not need to directly instantiate the Filter class. Instead, you can pass a filter array to the static methods ::find(array $filterArray), ::one(array $filterArray), or ::getCount(array $filterArray) of the relevant model class."

Automatic Data Transformation in SimpleObject Library

The SimpleObject Library provides a mechanism for automatic data transformation, which ensures validation and conversion of data when reading from and writing to a database. Below is detailed information about this functionality.

Goal

The goal of automatic data transformation is to validate and convert data from the database format to a format convenient for working in PHP, and vice versa. This includes converting date strings to DateTime class objects, as well as other data types such as numbers, strings, and JSON.

Built-in Data Types

The SimpleObject Library provides built-in transformers for various data types:

Usage

You can add your own custom transformers and bind them to models using the setDataTransformRule() method of the model class. For example:

If a field already has a transformer, it will be overwritten by the new one.

Transformer methods are automatically called when reading from the database or writing to it, ensuring automatic data conversion.

Examples

Example of using the built-in DateTimeTransformer in a base model:

This code binds the DateTimeTransformer transformer to the created_at and updated_at fields, allowing automatic conversion of date values when reading and writing to the database.

Advantages

Limitations

Automatic Relationships between Models

Automatic relationships between models in the SimpleObject library are limited to one-to-many and one-to-one relationships. If a table has a defined foreign key, it will be processed during model generation, and corresponding methods will be created.

One-to-Many Relationship

In a one-to-many relationship, if a table has a foreign key pointing to another table, the SimpleObject library automatically generates a method in the referencing model to retrieve related records.

For example, consider tables person and shop_order, where shop_order has a foreign key person_id referencing person(id). In the base model Person, the library automatically generates a method:

This method retrieves all shop orders associated with the person. You can optionally pass filters to narrow down the result set.

One-to-One Relationship

Similarly, in a one-to-one relationship, if a table has a foreign key pointing to another table, the SimpleObject library generates a method in the referenced model to retrieve the associated record.

Continuing with the example of tables person and shop_order, suppose shop_order has a foreign key person_id referencing person(id). In the base model ShopOrder, the library automatically generates a method:

This method retrieves the associated person for the shop order.

Usage

These automatically generated methods provide convenient access to related records without the need for manual coding. You can use them to streamline your database queries and simplify your application logic.

Note

These automatic relationships are based on foreign key constraints defined in the database schema. Ensure that your database schema accurately reflects the intended relationships between tables for the SimpleObject library to generate these methods correctly.

Caching with Runtime Cache

SimpleObject supports caching models data using runtime cache. This means that data is stored only during script execution and is not preserved between different requests to the application.

Caching Implementation

Examples of the load, save, and delete methods in the ActiveRecordAbstract class demonstrate caching when performing data operations. Here's how it works:

Method load: When loading a record from the database, the existence of the record in the cache is checked first. If the record already exists in the cache, it is retrieved from there, and no database query is executed. If the record is not found in the cache or a forced load is required ($forceLoad = true), then a database query is executed to load the record, and the retrieved data is cached for subsequent use.

Method save: When saving a record to the database, the data is also stored in the cache. If the record already exists in the database, its data is updated in both the database and the cache. If the record is inserted into the database for the first time, its data is also added to the cache.

Method delete: When deleting a record from the database, it is also removed from the cache.

Example Usage of Caching

Important Note

Contributing

We welcome contributions to SimpleObject! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on GitHub.

Thanks

Special thanks to ChatGPT for help with routine tasks.

License

SimpleObject is distributed under the MIT License with Custom Conditions. See the LICENSE file for more information.

Directory structure


All versions of simple-object with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
ext-pdo Version *
sanovskiy/traitlib Version ^2.0
sanovskiy/utility Version ^1.4
nette/php-generator Version ^4.1
league/climate Version ^3.8
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 sanovskiy/simple-object contains the following files

Loading the files please wait ....