跨多个模型的事务性 PDO 和 MVC

发布于 2024-11-07 09:32:27 字数 1250 浏览 0 评论 0原文

我正在构建一个站点,并且有多个跨多个模型的代码段,需要在事务中运行,因此如果其中一个失败,代码将回滚。

假设我有一个简单的表格来注册用户。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-11-14 09:32:27

正如我在评论中所说,外层可以与其他表交互:

模型

   1 DataBase  
     2  => UserTableDefinition  
             3  =>  UserDefaultInteraction 
                    4   => UserCustomFunctions  
                           5 =>  UserOuterLayer

和用户角色可以以相同的方式设置。现在所有模型都可以仅使用外层(5)相互交互;

既然您正在学习,我将为您提供一个详细的示例,以便您可以考虑使用强大的方法来处理您的模型,这只会使其变得更容易。
示例:

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();
       }    
    }
}

祝你学习顺利,我希望这是有道理的:)

As i said in my comment, the outer layer can interact with other tables:

Model

   1 DataBase  
     2  => UserTableDefinition  
             3  =>  UserDefaultInteraction 
                    4   => UserCustomFunctions  
                           5 =>  UserOuterLayer

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:

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文