English 中文(简体)
2. 模型和制图关系
原标题:Model & Mapper relationship

I currently work on a small application with models, mappers and controllers. My question is, (because I did not found any matching answer), how does the mapper interact with the model (& the controller), when we have the following situation.

$user = new UserModel();
$user->setId( 21 );
$userMapper = new UserMapper($user);
$userMapper->retrieve();

该模型将尽可能进行细微的工作,该模型有一张id,地图绘制者可以检索所需用户(并将其重新绘制成用户物体)。

My problem is, how can I wrap this code, I mean, this code is very raw and it is definitly not very recommended to be used in a controller. I want to shorten it, but I do not exactly know how:

public function view($id)
{
     $user->find($id); // this seems always to be tied with the user object/model (e.g. cakephp), but I think the ->find operation is done by the mapper and has absolutly nothing to do with the model
     $view->assign( user ,$user);
}

它应当更像:

public function view($id)
{
    $mapper = $registry->getMapper( user );
    $user = $mapper->find($id);
    // or a custom UserMapper method:
    # $user = $mapper->findById($id);
    $view->assign( user ,$user);
}

But this is much more code. Should I include the getMapper procedure within the parent controller class, so I can easily access $this->_mapper without explicitly calling it?

问题在于,我不想打破地图绘制模式,因此,《模型》不应直接通过<代码> model->find(),但我不想仅仅为首先创建地图绘制而制定许多法典,而且这样做等等。

我希望你们能理解我,我已经感到困惑不解,因为我对许多模式和绘图/模拟技术都很新。

最佳回答

http://martinfowler.com/eaaCatalog/serviceLayer.html” rel=“nofollow noreferer”>,例如:

class UserService
{
    public function findUserById($id)
    {
        // copied and adjusted from question text
        $user = new UserModel();
        $user->setId($id);
        $userMapper = new UserMapper($mapper);
        return $userMapper->retrieve();
    }
}

各位控制员直接通过服务处获得用户Model和用户Mapper。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签