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.
There are several way to handle this. You can have a factory class that will instantiate your Model class. Somthing in the line of:
Another way can be by setting a default connection to be used when the connection in the model class isn’t set.
Hope this helps