跨多个模型的带有MVC的事务性PDO [英] Transactional PDO with MVC across multiple models
问题描述
我正在构建一个站点,并且需要跨多个模型的多个代码段,这些代码段需要在一个事务中运行,因此,如果一个失败,代码将回滚.
I'm building a site and have multiple segments of code, across multiple models, that needs to run within a transaction so if one fails, the code will roll back.
说我有一个简单的表格来注册用户.
Say I have a simple form to register a user.
<form action="/register" method="POST">
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username" />
</div>
<div>
<label for="username">Password</label>
<input type="password" name="password" id="password" />
</div>
<div>
<label for="username">Email</label>
<input type="text" name="email" id="email" />
</div>
<div><input type="submit" name="submit" value="submit" /></div>
</form>
在我的系统中,创建用户后,我需要自动将其放置在角色中.
In my system when a user is created, I need to automatically place them in a role.
要使用User模型插入User表.然后,我需要插入出现在单独模型中的角色表.如果需要完成的所有工作都位于单独的模型中,那么我应在哪里创建要在多个模型之间传递的连接以使事务正常进行?
To insert into the User table, I use my User model. Then I need to insert the Role table which occurs in a separate model. If all work needed to be done lies within separate models, where do I create the connection to be passed across multiple models to allow the transaction to work?
// Start Transaction.
// Create new user based on posted variables. UserModel
// Add user to a given role. UserRoleModel -> Table contains UserId and RoleId
// Commit transaction.
也许让我感到困惑的是,所有创建用户的工作都应该放在我的用户模型中吗?即使工作不仅涉及User db表,还涉及很多内容?我的假设是数据库中的每个表都应具有一个模型类,而该模型类应仅在该表中起作用?我错了吗?
Maybe where I am confused is, should all work to create a user be in my user model? Even if the work spans across more than just the User db table? My assumption is that each table in the database should have a model class and that model class should do only work within that table? Am I wrong?
谢谢
推荐答案
正如我在评论中所说,外层可以与其他表进行交互:
As i said in my comment, the outer layer can interact with other tables:
型号
1 DataBase
2 => UserTableDefinition
3 => UserDefaultInteraction
4 => UserCustomFunctions
5 => UserOuterLayer
可以使用相同的方式设置用户角色.现在,所有模型都只能使用外层(5)进行交互;
And the User Role can be set up the same way. Now the all the models can interact with each other only using the outerlayer (5);
由于您正在学习,因此我将给yuou一个详细的示例,以便您可以考虑使用一种可靠的方式来处理模型,这将使它变得更加容易. 例如:
since you are learning, i will give yuou a detailed example so you can consider using a robust way of handling your model, and this simply make it easier. example:
class UserOuterLayer extends UserCustomFunctions {
public function createNewUser ($data) {
// create a new user from array
$user = new self();
$user->setName($data['name']);
$user->setUserName($data['username']);
$user->setPassword($data['password']);
$user->setRole($data['role']);
if ($user->save()) { // when the object is saved it assign a new value for $user->id
$role = new UserRole(); // accessing another model
$role->setUserId($user->getId()); // returns the new id from the saved object
$role->setRole($user->getRole());
$role->save();
}
}
}
祝您学习顺利,我希望这是有道理的:)
Good Luck in your learning, i hope this makes sense :)
这篇关于跨多个模型的带有MVC的事务性PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!