Php 数据映射器,模型
我有一个关于建模、培训课程的问题,有两个简单的规则不适合我的头脑,这些培训应该提供 在我的模型中,无论是“Php 数据映射器,模型,php,model-view-controller,model,connection,datamapper,Php,Model View Controller,Model,Connection,Datamapper,我有一个关于建模、培训课程的问题,有两个简单的规则不适合我的头脑,这些培训应该提供 在我的模型中,无论是“CustomerModel,VehicleModel”,我“必须”访问查询编写者的方法(SQLBuilder),但也可以访问数据库,该数据库不会设置映射器,因为映射器在工作台上工作,也不会设置映射器上方的银行,我的处境有点复杂 想象一下: <?php // database access, mapping abstract class Mapper { } abstr
CustomerModel
,VehicleModel
”,我“必须”访问查询编写者的方法(SQLBuilder)
,但也可以访问数据库,该数据库不会设置映射器,因为映射器在工作台上工作,也不会设置映射器上方的银行,我的处境有点复杂
想象一下:
<?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 { }
那么我如何使用映射器的save()
,delete()
,update()
。
在这种情况下,
getConnection()
方法必须获取默认连接,并返回类。有几种方法可以处理此问题。您可以有一个工厂类来实例化您的模型类。在以下方面有点问题:
class ModelLoader {
public function load($name)
{
$class = new $name();
$class->setConnection(/* set connection class */);
return $name;
}
}
$loader = new ModelLoader();
$customer = $loader->load('Customer');
另一种方法是,在未设置模型类中的连接时,设置要使用的默认连接
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) {
}
}
接口连接{}
类MySQL实现连接{}
类PgSQL实现连接{}
抽象类模型{
private$connection=null;
私有静态连接;
公共函数setConnection(Connection$Connection=null)
{
如果($connection==null){
如果(!self::$defaultConnection instanceof Connection){
抛出新异常(“缺少默认连接”);
}
$connection=self::$defaultConnection;
}
$this->connection=$connection;
退还$this;
}
公共函数getConnection()
{
如果($this->connection==null){
$this->setConnection();
}
返回$this->connection;
}
公共静态函数setDefaultConnection(Connection$Connection)
{
self::$defaultConnection=$connection;
}
}
类客户扩展模型{}
类车辆扩展模型{}
Model::setDefaultConnection(新MySQL);
$customer=新客户();
$customer->getConnection();
$vehicle=新车();
$vehicle->setConnection(新的PgSQL);
回声';
var_dump($客户);
自卸车(车辆);
//输出
对象(客户)#2(1){
[“连接”:“模型”:专用]=>
对象(MySQL)#1(0){
}
}
物体(车辆)#3(1){
[“连接”:“模型”:专用]=>
对象(PgSQL)#4(0){
}
}
希望这有帮助有几种方法可以解决这个问题。您可以有一个工厂类来实例化您的模型类。在以下方面有点问题:
class ModelLoader {
public function load($name)
{
$class = new $name();
$class->setConnection(/* set connection class */);
return $name;
}
}
$loader = new ModelLoader();
$customer = $loader->load('Customer');
另一种方法是,在未设置模型类中的连接时,设置要使用的默认连接
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) {
}
}
接口连接{}
类MySQL实现连接{}
类PgSQL实现连接{}
抽象类模型{
private$connection=null;
私有静态连接;
公共函数setConnection(Connection$Connection=null)
{
如果($connection==null){
如果(!self::$defaultConnection instanceof Connection){
抛出新异常(“缺少默认连接”);
}
$connection=self::$defaultConnection;
}
$this->connection=$connection;
退还$this;
}
公共函数getConnection()
{
如果($this->connection==null){
$this->setConnection();
}
返回$this->connection;
}
公共静态函数setDefaultConnection(Connection$Connection)
{
self::$defaultConnection=$connection;
}
}
类客户扩展模型{}
类车辆扩展模型{}
Model::setDefaultConnection(新MySQL);
$customer=新客户();
$customer->getConnection();
$vehicle=新车();
$vehicle->setConnection(新的PgSQL);
回声';
var_dump($客户);
自卸车(车辆);
//输出
对象(客户)#2(1){
[“连接”:“模型”:专用]=>
对象(MySQL)#1(0){
}
}
物体(车辆)#3(1){
[“连接”:“模型”:专用]=>
对象(PgSQL)#4(0){
}
}
如果您的模型“必须”直接(通过SQLBuilder
)访问数据库,希望这对您有所帮助。您使用了错误的模式:数据映射器。数据映射程序通常执行SQL操作,因此它的作用是:
[Models]---------[Storage]
`-----[Mapper]---´
在您的示例中,映射器似乎挂在某个地方,但不清楚它们是什么,因为您的模型需要直接访问存储:
我建议您实现代码示例,并讨论如何在映射器类型和超类型中解决更复杂的SQL交互
或者,具有每个模型的一些常见扩展的活动记录或一些行数据网关或表数据网关也可能适合您的需要。如果您的模型“必须”直接访问数据库(通过SQLBuilder
),则您使用了错误的模式:数据映射器。数据映射程序通常执行SQL操作,因此它的作用是:
[Models]---------[Storage]
`-----[Mapper]---´
在您的示例中,映射器似乎挂在某个地方,但不清楚它们是什么,因为您的模型需要直接访问存储:
我建议您实现代码示例,并讨论如何在映射器类型和超类型中解决更复杂的SQL交互
或者,具有每个模型的一些公共扩展的活动记录,或者一些行数据网关或表数据网关,也可能适合您的需要