This video will be available to
subscribers on Tue, Jun 17, 2025.
subscribers on Tue, Jun 17, 2025.
You must be a subscriber to view this video.
Jun 17, 2025
26m:22s
Database Model Abstractions
Classification
Software Engineering | Languages | PHP
Keywords
oop | php
About This Episode:
In this video, we take a big step toward cleaner, more maintainable code by introducing a Model abstraction layer—a pattern that reduces boilerplate and centralizes logic around database operations. This lands in version v20-db-models.
Here’s what gets done:
✅ Create a Models/Photo class
- Move the all() and find() logic out of PhotoController and into this model
✅ Create a Models/Review class
- Same deal: centralize data logic away from controllers
- Introduce a protected static ?string $table property
🧱 Extract shared logic into a Framework/Model base class:
- static table() throws by default—models must define their own table
- Implement reusable all() and find() methods
🔄 Refactor all() and find() with:
- static::query() → creates an instance of the model
- get() → used by all(), calls build_query() to prepare the SQL and params
- build_query() → generates the SQL from query state
🧠 Reimagine find() usage like this:
- static::query()->where('id', '=', $id)->first();
- Then implement:
- A where() method that collects conditions into $wheres
- Modify build_query() to handle WHERE clauses
- A first() method to return the first matching result
🚀 Refactor the PhotoController to use the new model APIs
- Add an insert() method on the base Model class for creating new records
By the end of this refactor, your models are smart, reusable, and expressive—freeing your controllers from repetitive SQL and focusing them on application logic. Pragmatic PHP is feeling pretty elegant right about now. 👌
Here’s what gets done:
✅ Create a Models/Photo class
- Move the all() and find() logic out of PhotoController and into this model
✅ Create a Models/Review class
- Same deal: centralize data logic away from controllers
- Introduce a protected static ?string $table property
🧱 Extract shared logic into a Framework/Model base class:
- static table() throws by default—models must define their own table
- Implement reusable all() and find() methods
🔄 Refactor all() and find() with:
- static::query() → creates an instance of the model
- get() → used by all(), calls build_query() to prepare the SQL and params
- build_query() → generates the SQL from query state
🧠 Reimagine find() usage like this:
- static::query()->where('id', '=', $id)->first();
- Then implement:
- A where() method that collects conditions into $wheres
- Modify build_query() to handle WHERE clauses
- A first() method to return the first matching result
🚀 Refactor the PhotoController to use the new model APIs
- Add an insert() method on the base Model class for creating new records
By the end of this refactor, your models are smart, reusable, and expressive—freeing your controllers from repetitive SQL and focusing them on application logic. Pragmatic PHP is feeling pretty elegant right about now. 👌
Short Bio:
Bob Cravens is a seasoned technology leader, software architect, and educator with a passion for empowering others through practical, real-world knowledge. As the founder of TekFoundry, Bob has dedicated his career to designing and delivering innovative software solutions that combine cutting-edge technology with elegant simplicity.
At TekCasts, Bob leverages over two decades of experience in software engineering, application architecture, and team leadership to create engaging, high-impact courses for developers and technology professionals. His instructional style is rooted in practical application, offering insights drawn from his extensive background in industries like medical devices and global technology consulting.
Bob’s expertise spans a variety of technologies and methodologies, including PHP/Laravel, FastAPI, and DevOps, as well as architecture best practices and team dynamics. Whether teaching foundational concepts or advanced strategies, he’s passionate about helping others turn complex challenges into streamlined solutions.
When not crafting elegant code or inspiring the next generation of tech innovators, Bob enjoys exploring the intersection of technology, business strategy, and creative problem-solving.
At TekCasts, Bob leverages over two decades of experience in software engineering, application architecture, and team leadership to create engaging, high-impact courses for developers and technology professionals. His instructional style is rooted in practical application, offering insights drawn from his extensive background in industries like medical devices and global technology consulting.
Bob’s expertise spans a variety of technologies and methodologies, including PHP/Laravel, FastAPI, and DevOps, as well as architecture best practices and team dynamics. Whether teaching foundational concepts or advanced strategies, he’s passionate about helping others turn complex challenges into streamlined solutions.
When not crafting elegant code or inspiring the next generation of tech innovators, Bob enjoys exploring the intersection of technology, business strategy, and creative problem-solving.