跨多个模型的事务性 PDO 和 MVC
我正在构建一个站点,并且有多个跨多个模型的代码段,需要在事务中运行,因此如果其中一个失败,代码将回滚。
假设我有一个简单的表格来注册用户。
<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>
在我的系统中,当创建用户时,我需要自动将他们放置在角色中。
为了插入用户表,我使用我的用户模型。然后我需要插入出现在单独模型中的角色表。如果需要完成的所有工作都位于单独的模型中,那么我在哪里创建要跨多个模型传递的连接以允许事务正常工作?
// Start Transaction.
// Create new user based on posted variables. UserModel
// Add user to a given role. UserRoleModel -> Table contains UserId and RoleId
// Commit transaction.
也许我感到困惑的是,创建用户的所有工作都应该在我的用户模型中吗?即使工作不仅仅涉及用户数据库表?我的假设是数据库中的每个表都应该有一个模型类,并且该模型类应该只在该表中工作?我错了吗?
谢谢
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.
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.
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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中所说,外层可以与其他表交互:
模型
和用户角色可以以相同的方式设置。现在所有模型都可以仅使用外层(5)相互交互;
既然您正在学习,我将为您提供一个详细的示例,以便您可以考虑使用强大的方法来处理您的模型,这只会使其变得更容易。
示例:
祝你学习顺利,我希望这是有道理的:)
As i said in my comment, the outer layer can interact with other tables:
Model
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);
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:
Good Luck in your learning, i hope this makes sense :)