Download the PHP package mattdwyercool/porm without Composer

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

PORM: a vanilla ORM for PHP

I think that Eloquent is too abstracted and feels bad once you go beyond single-table CRUD objects, Doctrine is too verbose, and neither of them are feasible to put into legacy projects.

PHP has PDOStatement::fetchObject() and I like to write my SQL manually, so I ended up developing a design pattern of sorts for reading data out of the database and now I'm formalising that into PORM ☺️

PORM works more like a "framework" by providing the mechanisms for object mapping and loading associated records, but let's you write your own SQL and whatever logic for loading those associations and casting data types.

Installation

composer require mattdwyercool/porm

Features

Database support

PORM supports PDO and MySQLi but leaves it up to you to manage them - you can use a Service Container, a global variable, or even the procedural mysqli_xxx() functions.

PORM includes a pair of optional helper facades that you can initialise during your app's bootstrapping with:

How it works

I haven't written proper docs yet so have a look at the examples/ folder for a demo or read below for a crash course.

But in a nutshell: define a class with properties like you normally would, then make it use \PORM\Model and define some static getter methods. Those getter methods should build a PDOStatement object whose columns match up with your class properties, and then call static::one() for a single-record getter or static::many() for an array.

one() and many() will call PDOStatement::fetchObject( static::class ) on the statement you give it, then scan the class for any relationships you've defined (see below). PORM unset()'s those relationship properties so that you keep IDE autocomplete, and relies on __get() to lazy-load them when called upon. When that happens, PORM will also load the related records for the "siblings" in your original static::many() call to avoid N+1 queries.

Object Mapping

Create a plain PHP class with your properties like:

That's it, but keep the data types as simple scalars (see below for non-scalar Type Casting).

Getter functions

You can define whatever model getters you need by adding static methods to your class that creates a PDOStatement or mysqli_result object and passes it to static::one() for single-record getters or static::many() for an array.

static::one() returns an instance of your class or null if the SQL query returned empty:

static::many() returns an array of instances:

Obviously writing the same SQL query every time is dumb so for best practice just move it to a helper method:

Type Casting

Under the hood static::one() and static::many() calls $stmt->fetchObject( static::class ) which works like this:

When an object is fetched, its properties are assigned from respective column values, and afterwards its constructor is invoked.

therefore if you have something like a JSON object or a CSV string of tags you can handle them in the constructor like this:

Joining multiple tables

Because you control the SQL you can do whatever you want:

You can use CTEs and the LATERAL keyword, and UNION's can be done a couple of ways:

  1. wrap them in a CTE and have one static::sql() method like:

  2. have multiple SQL helpers and assemble them in your getter method like:

  3. merge two model classes together in PHP like:

Polymorphism and Inheritance

PORM uses static everywhere so you can extend your class and just override the necessary methods.

I don't have a generic example for this yet, but I've done these before:

Because PORM models are just PHP classes and you write your own SQL the world is your oyster ❤️

Relationships

Relationships in PORM are protected from N+1 queries by default, and you define them using attributes:

Belongs To

On the child record you define the "local key" and the "remote class":

PORM will call Parent::list( $ids ) with the $parent_id's of the loaded child records.

You can manually specify the local key ($parent_id) and remote key ($id) on the BelongsTo attribute if PORM guesses them wrong based on the property type, and a custom method if you want something other than Parent::list().

Has One

This is the inverse of BelongsTo - on the parent record you just define the remote class:

PORM will call Child::fromParents( $parent_ids ) with the $id's of the loaded parent records. The method call is based on a naming convention, so if you had an Order parent model with a Shipment child relation it would call Shipment::fromOrders( ... ).

You can manually specify the local key ($id), remote key ($parent_id), and method name on the attribute here too.

Has Many

On the parent record you define an array and type-hint the remote class:

PORM will call Child::fromParents( $parent_ids ) with the $id's of the loaded parent records same as HasOne does, but plucks Child records into the $children array where $child->parent_id === $parent->id.

HasMany makes the same guesses as HasOne and you can manually specify the same properties, but because PHP doesn't have typing for arrays yet you have to specify the remote class manually and a PHPDoc to get IDE autocomplete working.

Writing to the DB

Right now the scope of PORM is just for reading from the DB but it's your code so you can do whatever you want.

I plan to add insert and update helpers to the DB class as well as some other utilities so check back later!

Contributing

Yeah go for it mate, no worries 😊


All versions of porm with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
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 mattdwyercool/porm contains the following files

Loading the files please wait ...