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(),但我不想仅仅为首先创建地图绘制而制定许多法典,而且这样做等等。
我希望你们能理解我,我已经感到困惑不解,因为我对许多模式和绘图/模拟技术都很新。