Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php OOP数据设置uu构造和SQL与多个类的连接问题_Php_Mysql_Class_Oop_Constructor - Fatal编程技术网

Php OOP数据设置uu构造和SQL与多个类的连接问题

Php OOP数据设置uu构造和SQL与多个类的连接问题,php,mysql,class,oop,constructor,Php,Mysql,Class,Oop,Constructor,因此,情况如下: 我有两个类=用户和博客 Blogs类扩展了Users类 Users类构造看起来有点像这样: public $users_id; public $users_blogs_id; public $users_first_name; public $users_last_name; /** * Sets the object's properties using the values in the supplied array * * @param assoc The pro

因此,情况如下:

我有两个类=用户和博客

Blogs类扩展了Users类

Users类构造看起来有点像这样:

public $users_id;
public $users_blogs_id;
public $users_first_name;
public $users_last_name;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

}

public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

}
$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Users::select_all($sql);
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------From Users Class----------------------*/
if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }


}
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*--------------------Column that I wanna join----------------------*/
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }


}
public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    use ConstructFunctions;

}
Blogs类构造函数如下所示:

public $users_id;
public $users_blogs_id;
public $users_first_name;
public $users_last_name;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

}

public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

}
$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Users::select_all($sql);
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------From Users Class----------------------*/
if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }


}
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*--------------------Column that I wanna join----------------------*/
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }


}
public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    use ConstructFunctions;

}
从中联接表的列是用户表中的“users\u blogs\u id”和blogs表中的“blogs\u id”

我的sql语句看起来有点像这样:

public $users_id;
public $users_blogs_id;
public $users_first_name;
public $users_last_name;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

}

public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

}
$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Users::select_all($sql);
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------From Users Class----------------------*/
if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }


}
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*--------------------Column that I wanna join----------------------*/
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }


}
public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    use ConstructFunctions;

}
现在,“selectall”函数应该创建一个新对象,该对象将运行构造函数并将找到的值存储在其各自的属性中,然后返回结果

问题是,如果我调用Users类select,它将只运行Users构造函数,并只向我发送带有找到的结果的Users类属性。但是,如果我调用Blogs类select,它将返回Blogs属性以及users属性,但是,只有Blogs属性将包含找到的结果,users属性将为空,因为Blogs构造函数不包含使用找到的结果设置users属性所需的代码集

我尝试过的事情:

创建多个查询

例如,当我从Users类调用user select时,我获取“Users\u blogs\u id”列值,然后运行另一个查询,从blogs表中选择blog,其中“blogs\u id=:Users\u blogs\u id”

复制部分博客/用户构造函数代码

因此,由于Blogs类的构造函数中缺少部分构造函数代码,我只需从Users类的构造函数中复制该部分代码,并将其粘贴到Blogs类中,如下所示:

public $users_id;
public $users_blogs_id;
public $users_first_name;
public $users_last_name;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

}

public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

}
$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Users::select_all($sql);
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------From Users Class----------------------*/
if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }


}
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*--------------------Column that I wanna join----------------------*/
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }


}
public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    use ConstructFunctions;

}
这将使用select函数中找到的结果设置users类和blogs类的属性,但最终我会重复代码,这也是我不想要的

仅添加要加入构造函数的列

在本例中,我想加入的列是users类中的“users\u blogs\u id”和blogs类中的“blogs\u id”

由于Blogs类是子类,我将users类中的“users\u Blogs\u id”构造代码添加到Blogs构造代码中,如下所示:

public $users_id;
public $users_blogs_id;
public $users_first_name;
public $users_last_name;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

}

public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

}
$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Users::select_all($sql);
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------From Users Class----------------------*/
if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }


}
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*--------------------Column that I wanna join----------------------*/
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }


}
public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    use ConstructFunctions;

}
然而,这最终导致我创建了多个sql查询,这是我想要避免的

使用特征(错误的方式)

所以我所做的就是创建一个trait,它包含所有类的所有构造代码。下面是一个例子:

trait ConstructFunctions{
    
    
    
    public function __construct($data=array())
    {
     /*---------------Blogs Construct--------------------*/
      if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------Users Construct------------------------*/

 if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

    }

}
然后我简单地调用trait,比如Blogs类,如下所示:

public $users_id;
public $users_blogs_id;
public $users_first_name;
public $users_last_name;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }

}

public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

}
$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Users::select_all($sql);
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*-----------------------From Users Class----------------------*/
if(isset($data['users_id']))
  {
   $this->users_id= $data['users_id'];
  }
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }
   if(isset($data['users_first_name']))
  {
   $this->users_first_name = $data['users_first_name'];
  }
   if(isset($data['users_last_name']))
  {
   $this->users_last_name = $data['users_last_name'];
  }


}
/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

/*--------------------Column that I wanna join----------------------*/
if(isset($data['users_blogs_id']))
  {
   $this->users_blogs_id= $data['users_blogs_id'];
  }


}
public $blogs_id;
public $blogs_title;
public $blogs_author;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    use ConstructFunctions;

}
现在这种方法对我最有效。它消除了代码的重复,允许我传递一个sql查询。但问题是,据我的同事说,这是施工人员的错误做法,他们阻止我使用这种方法

我的目标

使用联接通过单个sql查询存储和获取所有类的属性值。停止构造函数中代码的重复或至少在一定程度上减少代码的重复。不违反施工人员的法律


感谢您的每一个意见。非常感谢。如果您对我的问题有任何疑问,请务必告诉我。

您可以致电父构造函数:

// blog class
public function __construct($data=array())
{
    /* your code to set properties in blog */

    parent::__construct($data);
}

但我建议您使用并重新考虑您的类设计,例如类->实体->表:

class User // <---------------+
{                          // |
    // some fields            |
}                          // |
                           // |
class Blog                 // |
{                          // |
    // some fields            |
                           // |
    User $user;   // ---------+
}

class User//谢谢@Cid解决了我的问题。我将发布我为任何需要它的人提出的解决方案:

Blogs类有两个简单的更改:一个附加属性和设置propertys值的代码

public $blogs_id;
public $blogs_title;
public $blogs_author;

//New Property
public $users;

/**
 * Sets the object's properties using the values in the supplied array
 *
 * @param assoc The property values
 */

public function __construct($data=array()){

    if(isset($data['blogs_id']))
  {
   $this->blogs_id= $data['blogs_id'];
  }
   if(isset($data['blogs_title']))
  {
   $this->blogs_title = $data['blogs_title'];
  }
   if(isset($data['blogs_author']))
  {
   $this->blogs_author = $data['blogs_author'];
  }

//New property setting code

   $this->users = new Users($data);
  

}
sql保持不变,但是现在从Blogs类调用select语句

$sql = "SELECT * FROM users LEFT JOIN blogs ON users.users_blogs_id = blogs.blogs_id";
$data = Blogs::select_all($sql);

它现在将使用其构造函数使用找到的值设置Blogs类的属性,并创建一个新的Users对象,该对象将使用Users类的构造函数使用找到的值设置Users类的属性,并将其存储在“$Users”中属性,然后将包含适当数据的所有博客和用户属性发送回“$data”变量