简体   繁体   English

PHP OOP-实现2个类的最佳方法

[英]PHP OOP - best way to implement 2 classes

I have a simple DB with user data table, a controller data table and user_controller table that have the controllerID and the userId. 我有一个带有用户数据表,控制器数据表和user_controller表的简单DB,它们具有controllerID和userId。

In my PHP OOP (first time) implementation i have a class User and a class Controller. 在我的PHP OOP(第一次)实现中,我有一个User类和一个Controller类。

The functionality for the user is simple - he can login and logout and see a table of all his controllers. 用户的功能很简单-他可以登录和注销并查看其所有控制器的表。 I have an admin user who can see all users and their controllers, he can add/delete/edit controller to a user and also can add\\delete\\edit users. 我有一个管理员用户,可以查看所有用户及其控制器,他可以向用户添加/删除/编辑控制器,还可以添加\\删除\\编辑用户。

The Login functionality is implemented (DB class, user class and helpers class) 实现了登录功能(数据库类,用户类和助手类)

My problem is how and where should i build the functionality to add a controller to a user. 我的问题是如何以及在何处构建向用户添加控制器的功能。 Controller has an id (unique), a name(not unique) and password. 控制器具有一个ID(唯一),一个名称(非唯一)和密码。

User private fields are: 用户私有字段是:

private $_db,
        $_data, //all of this user data
        $_sessionName,
        $_cookieName,
        $_isLoggedIn;

and controller class: 和控制器类:

private $_db,
        $_data; //all of this controller data

I tried to implement function Create in controller: 我试图在控制器中实现功能Create:

public function create($fields = array()){
        if(!$this->_db->insert('controllers',$fields)){
            throw new Exception("Problem creating new controller");}}

and function createController in the user: 并在用户中使用createController函数:

public function addController($pass,$controller_name,$lat='',$lon=''){
    if ($pass && $controller_name){
        $controller = new Controller();

        $salt = Hash::salt(32);

        try{
            $controller->create(array(
                    'pass' => Hash::make($pass,$salt),
                    'salt' => $salt,
                    'controller_name' => $controller_name,
                    'lat' => $lat,
                    'lon' => $lon
                    ));
        //if success then we update user-controller table
            $controller_id = $controller->find($controller_name)->data()->id;
            $userID = $this->data()->ID;
            $fields = array(
                'UserID' => $userID,
                'ControllerID' => $controller_id
                );

            if(!$this->_db->insert('user_controllers',$fields)){
                throw new Exception("Problem creating controller for user");
            }
        }
        catch(Exception $e){
            throw new Exception('There was a problem creating new controller'); 
        }
    } else {
        throw new Exception('No name or password had been given');
    }

Generally it is working. 通常它正在工作。 But there have been couple problems: 但是有一些问题:

  1. The id of the controller is created automatically by the DB so i dont know it when i create it. 控制器的ID由数据库自动创建,因此在创建时我不知道它。 I cant later find the controller by name because i can have a lot of controllers with this name 我以后找不到按名称的控制器,因为我可以有很多使用此名称的控制器

  2. I am not sure this is the proper way to handle the relationship between user and controller 我不确定这是否是处理用户与控制器之间关系的正确方法

  3. i have trouble of implementing "show me all your controllers" for the user. 我很难为用户实施“向我展示所有控制器”。

Can you please tell me what is the correct way to implement this relationship and how to make the createController and ShowControllers of the user? 您能告诉我实现这种关系的正确方法是什么,以及如何使用户的createController和ShowControllers吗?

Your structure seems to be correct. 您的结构似乎是正确的。 You need a users table, a controllers table and a user_controller table as you mentioned which maps the relationships between the two (user has many controllers). 您需要一个用户表,一个控制器表和一个user_controller表,它们映射了两者之间的关系(用户具有许多控制器)。

Solutions for your problems: 解决问题的方法:

  1. When you insert data, you need to retrieve the inserted ID of the new record to use in your mapping relationship. 插入数据时,需要检索新记录的插入ID以在映射关系中使用。 I'm not sure in your example if you're written this MVC structure yourself or otherwise; 在您的示例中,我不确定您是自己编写还是以其他方式编写此MVC结构; it looks a little like CodeIgniter. 它看起来有点像CodeIgniter。 If it is a CI application, here's an example of how to get the last insert ID . 如果它是CI应用程序, 这是如何获取最后一个插入ID的示例 If it's a bespoke application, you can use $mysqli->insert_id (mysqli) or $pdo->lastInsertId(); 如果是定制应用程序,则可以使用$mysqli->insert_id (mysqli)$pdo->lastInsertId(); for PDO. 用于PDO。

  2. What you're doing is correct. 你在做什么是对的。

  3. In a "show me all your controllers" for the user scenario, you will query the user_controller table to get a list of all the controller relationships, and you'll use a join (inner might suit, for a mandatory successful join). 在针对用户场景的“显示所有控制器”中,您将查询user_controller表以获取所有控制器关系的列表,并使用联接(内部可能适合强制成功的联接)。 You specify that you want to return controller.* in your select fields, and you'll end up with a record for each controller and ignore the user controller fields (if you don't need them). 您在选择字段中指定要返回controller.* ,最后将得到每个控制器的记录,并忽略用户控制器字段(如果不需要)。 An example SQL query might look like this: 一个示例SQL查询可能看起来像这样:

     SELECT controller.* FROM user_controller UC SELECT控制器。* FROM user_controller UC\nINNER JOIN controller ON controller.id = user_controller.controller_id 内连接控制器打开controller.id = user_controller.controller_id\nWHERE user_controller.user_id = [YOUR_USER_ID_HERE] WHERE user_controller.user_id = [YOUR_USER_ID_HERE] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM