PHPackages                             ivir3zam/phalcon-model-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ivir3zam/phalcon-model-middleware

ActiveLibrary

ivir3zam/phalcon-model-middleware
=================================

This library is for connecting models in phalcon like UML Class Diagrams. merging two models and create one single model. this is an implementation of polymorphic relationship

0.9.9(9y ago)148MITPHPPHP &gt;=5.4.0

Since Apr 20Pushed 9y ago2 watchersCompare

[ Source](https://github.com/IVIR3zaM/PhalconModelMiddleware)[ Packagist](https://packagist.org/packages/ivir3zam/phalcon-model-middleware)[ Docs](https://github.com/IVIR3zaM/PhalconModelMiddleware)[ RSS](/packages/ivir3zam-phalcon-model-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)DependenciesVersions (11)Used By (0)

Phalcon-Model-Middleware
========================

[](#phalcon-model-middleware)

Phalcon php framework is a powerfull framework for creating big scale projects that focuses on optimization. but there is no polymorphic relationship between models. I solve this problem by making a trait that can be placed in middleware of two classes o join them as a single model. in this presentation we can have one model that have actually two or more tables in database. using this trait is like playing with knife! you must know that you ar doing.

Sample Case #1 (Diffrent Types of Users)
----------------------------------------

[](#sample-case-1-diffrent-types-of-users)

### The Problem

[](#the-problem)

Imaging a system that have a parent class named Users. each user can have login to system, so there is a table named users. we have another class named Customers that extends Users, so Customers can login to system and submit order some products. we have another class named Operators that extends Users, so they can login to system but they cann't submit order but they can view admin area. in UML these are 3 simple classes with 2 extends. in database these can have 3 tables with relations. but in phalcon using this 3 tables are confusing. everytime you must join tables to find records that you want. but with this triat all things going right.

### The Solution

[](#the-solution)

you must create 3 tables named users,customers and operators all tables have a primary key named id. but customers and operators id field have a foreign key with users.id field. then create 3 models : Users, Customers, Operators. then create an middle class named UsersMiddleware. Users must extends Phalcon\\Mvc\\Model. UsersMiddleware is an abstract class and extends Users and use IVIR3zaM/Phalcon-Model-Middleware/ModelsMiddleware as a trait. Customers and Operators extends UsersMiddleware. know you can add a function named getUniqueField() to UsersMiddleware to specify what unique field is the foreign key of all 3 tables. in Users model you must have at least one field that specify the type of User. lets call that type. in our case type can be Customer or Operator. know you must add a function named getCustomFields() in Customers and Operatos that return what columns must use for detecting the type of users and what data they must to have. for Customers in can return \['type' =&gt; 'Customer'\] and for Operators it can return \['type' =&gt; 'Customer'\]. your done!

### Use-Case

[](#use-case)

know you can find customers like this: `Customers::find(['id > 10'])` and get customers objects in full filled attributes. but there is a single problem. my trait use afterFetch() hook to fill all attributes of your Customers objects. so when you loop on your Customers for each Customer one SELECT query will made up to fetch Users data of that Customer! so if you wanna loop on too many records don't use this function instead, use fullFind function like this `Customers::fullFind(['id > 10'])`. in this state all data will fetched with a single query by a join on two tables. but in this case fullFind will return an array of Phalcon\\Mvc\\Model\\Row objects! this is for phalcon policy that return Row objects on joined queries. you can use this objects instead of original ones in view, but if you need the actual objects to do some jobs like updating data simply do it like this:

```
$customers = Customers::fullFind();
foreach($customers as $customer) {
    $customer->name = ($customer->gendre == 'Man'?'Mr.':'Mrs.').$customer->name;
    Customers::toObject($customer)->save();
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Recently: every ~21 days

Total

10

Last Release

3573d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5f9922eaf03048c0fd429f6c01cc0d5168489a63cf1f60c6735caf5f4678ab7c?d=identicon)[IVIR3zaM](/maintainers/IVIR3zaM)

---

Top Contributors

[![IVIR3zaM](https://avatars.githubusercontent.com/u/4234196?v=4)](https://github.com/IVIR3zaM "IVIR3zaM (14 commits)")

---

Tags

modelphalconpolymorphic

### Embed Badge

![Health badge](/badges/ivir3zam-phalcon-model-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/ivir3zam-phalcon-model-middleware/health.svg)](https://phpackages.com/packages/ivir3zam-phalcon-model-middleware)
```

###  Alternatives

[rinvex/laravel-categories

Rinvex Categories is a polymorphic Laravel package, for category management. You can categorize any eloquent model with ease, and utilize the power of Nested Sets, and the awesomeness of Sluggable, and Translatable models out of the box.

470161.6k3](/packages/rinvex-laravel-categories)[rinvex/laravel-tenants

Rinvex Tenants is a contextually intelligent polymorphic Laravel package, for single db multi-tenancy. You can completely isolate tenants data with ease using the same database, with full power and control over what data to be centrally shared, and what to be tenant related and therefore isolated from others.

823.4k10](/packages/rinvex-laravel-tenants)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
