gpt4 book ai didi

php - 如何使用数组中的键 [table.attribute] mysqli_result::fetch_array?

转载 作者:可可西里 更新时间:2023-11-01 08:39:39 25 4
gpt4 key购买 nike

我正在尝试创建一个通用方法来自动从这样的查询中实例化对象:

SELECT town.*, content.*, user.*
FROM townhub.content
LEFT JOIN town ON content.townReceiver = town.id_town
LEFT JOIN user ON content.author = user.id_user

我想要构建的方法应该返回 3 种类型的对象:TownUserContent 到一个数组中。我想到了类似的事情:

protected function build_objects($result, Array $classes) {
$data = array();
$i = 0;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

foreach ($classes as $class) {
$$class = new $class;
$$class = $$class->fill_object($$class, $row);
$data[$i][$class] = $$class;


}
$i++;
}
return $data;
}

然后,在每个类中,一个这样的方法:

public function fill_object($object, Array $row) {
$attributes = get_object_vars($object);
foreach ($row as $attribute => $value) {
foreach ($attributes as $objAtt => $emptyValue) {
if ($attribute == $objAtt) {
$object->$attribute = $value;
}
}
}
return $object;
}

实际上,这是我想要的,下面的数组(使用 build_objects() 中的 var_dump($data) 打印):

array (size=4)
0 =>
array (size=3)
'Content' =>
object(Content)[4]
protected 'id_content' => string '1' (length=1)
public 'title' => string 'Hello World!' (length=10)
public 'description' => string 'Hello world description' (length=43)
public 'category' => string '1' (length=1)
public 'date' => string '2015-01-01' (length=10)
public 'townReceiver' => string '1' (length=1)
public 'author' => string '1' (length=1)
private 'dbHost' (Model) => string 'localhost' (length=9)
private 'dbUser' (Model) => string 'root' (length=4)
private 'dbPass' (Model) => string 'root' (length=4)
private 'dbName' (Model) => string 'townhub' (length=7)
private 'conn' (Model) => null
'Town' =>
object(Town)[5]
protected 'id_town' => string '1' (length=1)
public 'name' => string 'Isaac' (length=5)
public 'population' => string '750' (length=3)
private 'dbHost' (Model) => string 'localhost' (length=9)
private 'dbUser' (Model) => string 'root' (length=4)
private 'dbPass' (Model) => string 'root' (length=4)
private 'dbName' (Model) => string 'townhub' (length=7)
private 'conn' (Model) => null
'User' =>
object(User)[6]
protected 'id_user' => string '1' (length=1)
protected 'dni' => string '20011225' (length=9)
private 'password' => string '1234' (length=4)
public 'name' => string 'Isaac' (length=5)
public 'firstSurname' => string 'Surname1' (length=5)
public 'secondSurname' => string 'Surname2' (length=5)
public 'birthdate' => string '0000-00-00' (length=10)
public 'gender' => string 'H' (length=1)
public 'email' => string 'isaac@mail.com' (length=19)
public 'isAdmin' => string '1' (length=1)
public 'id_town' => string '1' (length=1)
private 'dbHost' (Model) => string 'localhost' (length=9)
private 'dbUser' (Model) => string 'root' (length=4)
private 'dbPass' (Model) => string 'root' (length=4)
private 'dbName' (Model) => string 'townhub' (length=7)
private 'conn' (Model) => null

问题是,当我 fech_array(在 fill_object() 方法中)城镇名称被用户名称覆盖时;所以我不能得到镇的名字。简单的解决方案是更改数据库和类上的属性名称;但我认为这将是一个糟糕的解决方案......请记住,数据库和类上的属性名称应该相同,因此查询中的 alias 是不可能的。

有什么方法可以通过 fetch_array 获取一个键为 [table.attribute] 而不是 [attribute] 的数组?
如果无法实现我想做的事情,我也想知道更好的方法。

最佳答案

 <?php

class DataCollectionHelper
{
/**
* $result var is the result of:
* "SELECT *
* FROM townhub.content".
*
* As you left join your tables, it may appear that you haven't any towns and users
* for particular content.
*
* @param $result
*
* @return array
*/
protected function build_objects($result)
{
$data = array();
$i = 0;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$contentObject = new Content();
$contentObject->fill_object($row);
$data[$i]['Content'] = $contentObject;
$data[$i]['User'] = $this->getUserByContentAuthor($contentObject->getAuthor());
$data[$i]['Town'] = $this->getTownByContentByTownReceiver($contentObject->getTownReceiver());
$i++;
}

return $data;
}

/**
* @param integer $townReceiver
*
* @return Town
*/
private function getTownByContentByTownReceiver($townReceiver)
{
/**
* Here you have to get town data by your $townReceiver - property
* and return the object of Town
*/
}

/**
* @param integer $author
*
* @return User
*/
private function getUserByContentAuthor($author)
{
/**
* The same here for users
*/
}
}

class Content
{
/**
* @var integer
*/
protected $content_id;
/**
* @var string
*/
public $title;

/**
* @var integer
*/
public $townReceiver;

/**
* @var string
*/
public $description;

/**
* @var integer
*/
public $author;
/* other vars */

/**
* @param array $row
*
* @return $this
*/
public function fill_object(array $row)
{
/*
* It's not the best approach, to do like that.
* It's better to use setters or just set your properties
* $this->title = $row['title']; etc
*
* Still you could use $this instead of your $object variable
*/
$attributes = get_object_vars($this);
foreach ($row as $attribute => $value) {

foreach ($attributes as $objAtt => $emptyValue) {
if ($attribute == $objAtt) {
$this->$attribute = $value;
}
}
}

return $this;
}

/**
* @return int
*/
public function getTownReceiver()
{
return $this->townReceiver;
}

/**
* @return int
*/
public function getAuthor()
{
return $this->author;
}
}

关于php - 如何使用数组中的键 [table.attribute] mysqli_result::fetch_array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com