Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:01:18+00:00 2026-05-31T13:01:18+00:00

This is my problem, I have a tiny PHP MVC framework i built. Now

  • 0

This is my problem, I have a tiny PHP MVC framework i built.
Now when Im in Controller, i should be able to load a Model.
I have Models named like database tables like Users.php, Pages.php etc.

All Controllers extend BaseController, and all Models extend BaseModel, this way I can have some methods available to all Controllers. Like from any Controller I can use loadModel method like this:

$usersModel = $this->loadModel("Users");

Now $usersModel will be object that represents users database table, and from there I should open database connection, and fetch, update, delete stuff from users table.

To get database connection from my Models, baseModel has method loadDatabase() and I use it like this:

$database = $this->loadDatabase();

This would return class that is thin layer around PDO so from there I can use something like this:

$data = $database->fetchAssoc("SELECT * FROM users WHERE name = ?", array("someone"));

This is how I would return some $data from Model to the Controller.

So basicly, Controller can load a model, and then call methods on that model that would return some data or update or delete etc.

Now, Controller can load more then one model. And each model should load database, and this is where it gets complicated. I need only one connection to the database.

This is how loadDatabase method looks:

    public function loadDatabase()
    {
        // Get database configuration  
        require_once("Application/Config/Database.php");

        // Build configuration the way Database Object expects it
        $dns      = "{$db_config['db_driver']}:host={$db_config['db_host']};dbname={$db_config['db_name']}";
        $username = $db_config['db_user'];
        $password = $db_config['db_pass'];
        $options  = $db_config['db_options'];

        // Return new Database object
        return new \Core\Database\Database($dns, $username, $password, $options);

    }

So before I load a Database object, i must load configuration for database connection, as Database objects __constructor expects it. Then I instanciate new database object and return it.

Now Im stuck and I dont know is this the right way to loadDatabase from model?
How should I set this up, how should I load database from inside the model so there is always only one instance of database object. Beacuse if I do something like this from Controller:

$users = $this->loadModel("Users");
$pages = $this->loadModel("Pages");

$users->doSomethingThatNeedsDatabase();
$users->doSomethingThatNeedsDatabase();
$pages->doSomethingThatNeedsDatabase();

I would create 3 database objects 🙁

So my question is, how should I load Database from inside the Models, and how should that method look in BaseModel?
What if I would like to use 2 databases, I will have some big problems with this setup.
How can I achive this without using singleton pattern?

At the moment, I also have something like this:

public function getDatabase()
{

    $reg = \Core\Registry\Registry::getInstance();
    $db = $reg->getObject("database");
    if(!isset($db))
    {
        require_once("Application/Config/Database.php");

        $dns      = "{$db_config['db_driver']}:host={$db_config['db_host']};dbname={$db_config['db_name']}";
        $username = $db_config['db_user'];
        $password = $db_config['db_pass'];
        $options  = $db_config['db_options'];                

        $db = new \Core\Database\Database($dns, $username, $password, $options); 

        $reg->setObject('database', $db);
    }
    return $reg->getObject('database');

}

This is Registry pattern, where I could hold shared objects. So when Model asks for DB connection I could check if DB Class is in Registry, and return it, if not I would instaciate and then return… The most confusing thing is that I need to load configuration array…

So what is the best way, to load Database from Models?

Thanks for reading, this was a very long question, but this is bothering me so much, i hope someone could help me with this one… Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:01:20+00:00Added an answer on May 31, 2026 at 1:01 pm

    You are going in the wrong way about solving this.

    Instead of each time manually making a new “Model” and then configuring it, you should create a structure that does it for you ( extremely simplified version ):

    class ModelFactory
    {
        protected $connection = null;
        // --- snip --
    
        public function __construct( $connection )
        {
            $this->connection = $connection;
        }
        // --- snip --
    
        public function buildMapper( $name )
        {
            $instance = new {$name}( $this->connection );
            return $instance;
        }
        // --- snip --
    
    }
    

    This class you would be using in index.php or bootstrap.php , or any other file you use as entry point for your application:

    // at the bootstrap level
    $modelFactory = new ModelFactory( new PDO(...) );
    
    // i am assuming that you could get $controllerName 
    // and $action from routing mechanism
    $controller = new {$controllerName}( $modelFactory );
    $controller->{$action}();
    

    The main problem you have is actually cause by misunderstanding what Model is. In a proper MVC the Model is a layer, and not a specific class. Model layer is composed from multitude of class/instances with two major responsibilities:

    • domain business logic
    • data access and storage

    The instances in first group are usually called Domain Objects or Business Objects (kinda like situation with geeks and nerds). They deal with validations, computation, different conditions, but have no clue how and where information is stored. It does not change how you make an Invoice , whether data comes from SQL, remote REST API or a screenshot of MS Word document.

    Other group consists mostly of Data Mappers. They store and retrieve information from Domain Objects. This is usually where your SQL would be. But mappers do not always map directly to DB schema. In a classic many-to-many structure you might have either 1 or 2 or 3 mappers servicing the storage. Mappers usually one per each Domain Object .. but even that is not mandatory.

    In a controller it would all look something like this.

    public function actionFooBar()
    {
        $mapper = $this->modelFactory->buildMapper( 'Book' );
        $book = $this->modelFactory->buildObject( 'Book' );
        $patron = $this->modelFactory->buildObject( 'Patron' );
    
        $book->setTitle('Neuromancer');
        $mapper->fetch( $book );
    
        $patron->setId( $_GET['uid']);
    
        if ( $book->isAvailable() )
        {
            $book->lendTo( $user );
        }
    
        $mapper->store( $book );
    
    }
    

    Maybe this will give you some indications for the direction in which to take it.

    Some additional video materials:

    • Advanced OO Patterns (slides)
    • Global State and Singletons
    • Don’t Look For Things!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
I am trying to learn ASP.NET MVC and I hit this problem: I have
Ok I have this problem I'm trying to use Jquery to load a partial
NOTE: I have solved the majority of this problem but have run into a
I am wondering how you would approach this problem I have two Taxrates that
Ok I have this problem that I've never had before, it's really bugging me.
So I have this problem with strings and switch-case, and I'll try to keep
I have this problem I'm hoping someone knows the answer to. I have an
I have this problem where I open Visual Studio and the internal windows are
i have this problem to find a particular xml node l have post this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.