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

  • SEARCH
  • Home
  • 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 6936625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:15:28+00:00 2026-05-27T12:15:28+00:00

I have a question about a ‘ modeling, training ‘ classes, there are two

  • 0

I have a question about a ‘modeling, training‘ classes, there are two simple rules which do not fit in my head, and that such training should provide,

in my model, be it ‘CustomersModel, VehicleModel‘ I ‘have’ to have access to the methods of the writer of queries (SQLBuilder), but also have access to the database, which would not set the mapper as the mapper works on the bench, and not the bank over the mapper, so complicated a little by my situation.

Imagine the following:

<?php
   // database access, mapping
   abstract class Mapper { }
   abstract class Model { } 


   interface Connection { } // to recognize it's a database access 
   class MySQL implements Connection { }
   class PgSQL implements Connection { } 

    // SQL Helper, some methods to write a update, insert, delete, select query
   class SQLBuilder { }

   // my custom models
   class Customers extends Model { }

visualizing this structure, the ‘Customers’, already has access to the mapper, in which case I would have a property ‘$_table‘ (among others) in the Customers table to recognize that this model works, but .. I do not have access to the writer of queries, and not to the database, for this, I thought of a method ‘main()‘, where setaria connection to the database, using the ‘setDatabaseConnection ( Connection $connection )‘ but the code would be often repeated, just imagine for N models, all using -> setDatabaseConnectiom, and the entire application uses only one database? ..

My question is basically that, provide access to the database without using setter method in model (Customers, Vehicle), and the writer of queries right away.

Way i wan’t to use is ..

<?php

       // my models
       class Customers extends Model { 

              // method of mapper does'nt implement
              public function getByCity ( $city ) {
                     $query = $this->select()->from($this->_table)->where('city = ?', $city);
                     $resultSet = $this->getConnection()->query( $query );
                     if ( $resultSet->rowCount() > 1 ) {
                            return $resultSet->fetchAll();
                     } else return $resultSet->fetch();
              }

       }

so how can I use the save (), delete (), update () the mapper.
in this case, the getConnection () method would have to get the default connection, and return the class.

  • 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-27T12:15:29+00:00Added an answer on May 27, 2026 at 12:15 pm

    There are several way to handle this. You can have a factory class that will instantiate your Model class. Somthing in the line of:

    class ModelLoader {
    
        public function load($name)
        {
            $class = new $name();
            $class->setConnection(/* set connection class */);
            return $name;
        }
    }
    
    $loader = new ModelLoader();
    $customer = $loader->load('Customer');
    

    Another way can be by setting a default connection to be used when the connection in the model class isn’t set.

    interface Connection {}
    class MySQL implements Connection {}
    class PgSQL implements Connection {}
    
    abstract class Model {
    
        private $connection = null;
    
        private static $defaultConnection;
    
        public function setConnection(Connection $connection = null)
        {
            if ($connection === null) {
                if (!self::$defaultConnection instanceof Connection) {
                    throw new Exception('Default connection missing.');
                }
                $connection = self::$defaultConnection;
            }
            $this->connection = $connection;
    
            return $this;
        }
    
        public function getConnection()
        {
            if ($this->connection === null) {
                $this->setConnection();
            }
            return $this->connection;
        }
    
        public static function setDefaultConnection(Connection $connection)
        {
            self::$defaultConnection = $connection;
        }
    }
    
    class Customers extends Model { }
    class Vehicle extends Model { }
    
    
    Model::setDefaultConnection(new MySQL);
    
    $customer = new Customers();
    $customer->getConnection();
    
    $vehicle = new Vehicle();
    $vehicle->setConnection(new PgSQL);
    
    echo '<pre>';
    var_dump($customer);
    var_dump($vehicle);
    
    // output
    object(Customers)#2 (1) {
      ["connection":"Model":private]=>
      object(MySQL)#1 (0) {
      }
    }
    object(Vehicle)#3 (1) {
      ["connection":"Model":private]=>
      object(PgSQL)#4 (0) {
      }
    }
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about this question . I posted a reply there but
I have question about my code which does the Hoare Partition Method. Here is
i have question about two different Bundle object in below methods : onSaveInstanceState(Bundle outState);
I have a question about the compliance mode. What happens with my class which
Hello I have question about android spinner I have spinner which is populated by
Hello I have question about android spinner I have spinner which is populated by
I have question about global variable in Objective-C. I have two .m files :
I'm learning asp.net. I have question about example buttons I can use two types
Have a question about sending data between classes and activities. I currently have a
I have a question about local classes in Java (classes that declares in the

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.