[英]Table joins accross multiple database tables and objects in PHP and MySQL
I have 4 classes, each with their own database tables: 我有4个类,每个类都有自己的数据库表:
GlobalObject has 3 properties GlobalObject具有3个属性
Image, Project and Page have many different fields, they ALL have the following though: 图像,项目和页面具有许多不同的字段,但是它们都具有以下内容:
At the moment, my class structure has Images, Projects and Pages as subclasses of the GlobalObject class, this allows the 3 subclasses access to the variables that are required of them, datetime_added etc. 目前,我的类结构将Images,Projects和Pages作为GlobalObject类的子类,这允许3个子类访问它们所需的变量,datetime_added等。
How should I set the properties using PHP and MySQL? 如何使用PHP和MySQL设置属性? At the moment, ALL of the fields (including those in the global_object table) are in each section's table (except global_object_id which cannot exist without the new table - and is why I need to do this), So i request the data from the Image table for example, which includes all the properties in the parent class and is set using parent::set_by_row($database_args), and $this->set_by_row($database_args); 目前,所有字段(包括global_object表中的字段)都在每个节的表中(global_object_id除外,没有新表就无法存在-这就是为什么我需要这样做),所以我从Image请求数据例如,该表包括父类中的所有属性,并使用parent :: set_by_row($ database_args)和$ this-> set_by_row($ database_args)进行设置。
At the moment I do the following (Using Image as an example): 目前,我执行以下操作(以Image为例):
class Image extends GlobalObject
{
$id;
$title;
function set_by_id($id)
{
// Select Q from Image table
$this->set_by_row($db_result);
}
function set_by_row($args)
{
parent::set_by_row($args);
// Set class properties
}
}
To reiterate: I want to be able to do the following: 重申:我希望能够执行以下操作:
$image = new Image()
$image->set_by_global_object_id(23);
And the image be set, including everything in the parent class (which is stored in a separate table) 并设置图像,包括父类中的所有内容(存储在单独的表中)
Please ask away if any of that is unclear. 请问是否不清楚。
A very convenient way is offered by those "magic methods", like __get and __set. 这些“魔术方法”(例如__get和__set)提供了一种非常方便的方法。 The following example provides read-only access to the data, but can easily be extended with a __set method. 以下示例提供对数据的只读访问,但可以使用__set方法轻松扩展。
abstract class GlobalObject {
protected $_data = array();
/* Magic Method: Enable read access via $this->name */
public function __get($key) {
if (isset($this->_data[$key])) {
return $this->_data[$key];
}
throw new Exception('Property "'. $key .'" not found.');
}
protected function _setFromArray(array $data) {
// .. do whatever needs to be done with the data ..
$this->_data = $data;
}
}
Now just extend this class: 现在只需扩展此类:
class Image extends GlobalObject {
public function setByGlobalId($id) {
$result = mysql_query('SELECT i.*, g.* FROM images AS i
LEFT JOIN global_objects AS g
ON g.global_object_id = i.global_object_id
WHERE i.global_object_id = '. intval($id) .' LIMIT 1');
// @todo check if result is empty
$data = mysql_fetch_assoc($result);
$this->_setFromArray($data);
}
}
And a simple example: 还有一个简单的例子:
$image = new Image();
$image->SetByGlobalId(42);
echo $image->added_by;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.