- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试创建一个通用方法来自动从这样的查询中实例化对象:
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 种类型的对象:Town
、User
和 Content
到一个数组中。我想到了类似的事情:
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/
是否有时不调用 mysqli_result::free、mysqli_result::close、mysqli_result::free_result 可能更有益code> 尤其是当页面本身很短时,因
这个问题在这里已经有了答案: Fatal error: Call to undefined function mysqli_result() (2 个回答) 5年前关闭。 我有问题 mysqli_re
这是代码。当我执行它时,它显示错误消息“可捕获的 fatal error :类 mysqli_result 的对象无法转换为字符串”请帮我解决这个问题。 $sql = "SELECT dname FR
我在扩展 mysqli_result 类时遇到问题。 我正在尝试使用自定义类扩展mysqli_result 类。这是我的代码: class mysqli_result_extended extends
我有一个用 PHP 编写的查询,该查询返回表的主键(该表存储在数据库中的表数组中): $link:与数据库的连接 $tables:数据库表数组 $x:循环变量 $primaryKey = mysqli
我有大约 75 个 php 现有脚本可以访问 mysql 数据库,类似于此伪代码: $query="SELECT * from table"; $result=mysqli_query($conn,$
当 mysqli_query 被执行时,PHP 从数据库(MySQL)接收结果集,我认为这是一个对象(mysqli_result 类)。 这个 mysqli_result 类是否在其属性中携带来自数据
如果用户名被占用,我正在检查数据库,但我不断收到此错误“在第 9 行调用未定义的函数 mysqli_result()。” 可能因为我正在使用 jQuery 处理来自不同文件的输入并用它检查它,它说它
我有这个错误 Warning: mysqli_fetch_array() [function.mysqli-fetch-array]: Couldn't fetch mysqli_result in
这个问题在这里已经有了答案: Object of class mysqli_result could not be converted to string (5 个答案) 关闭去年。 我请求 Goo
我已经扩展了 PHP 的 mysqli 类,它工作正常。但是如何让它在查询时返回自定义结果对象(或用于插入/更新/删除等的 bool 值)? namespace MyApp; class MySQLi
这个问题在这里已经有了答案: Object of class mysqli_result could not be converted to string (5 个答案) 关闭 1 年前。 我是网络
抛出以下错误: Fatal error: Trying to clone an uncloneable object of class mysqli_result 当我尝试直接对 mysql 查询结果
我有一个验证用户登录的功能。 $login_query = mysqli_query($GLOBALS['conn'], "SELECT COUNT(`user_id`) as `count`,`us
根据我的研究,我了解到应该在操作/使用查询数据后释放结果集数据。我正在尝试在以下代码上下文中使用 mysqli_result::free pdo: if ($result) { $change
我已经搜索并尝试了不同的解决方案大约 3 个小时,现在我正在放弃并发布这个问题。 我有以下代码; prepare($sql); $stmt->bind_param('iisssi', $trackID
我正在尝试将此代码从 Java 复制到 php 中,但我无法准确弄清楚如何获取行/列的内容? 这是java代码: ResultSet resultsRS = statement.executeQuer
这个问题在这里已经有了答案: Object of class mysqli_result could not be converted to string (5 个答案) 关闭 1 年前。 我遇到了
我有一个 mysqli_result: $stmt = $db->prepare("SELECT * FROM customer"); $stmt->execute(); $result = $stm
我收到错误: Object of class mysqli_result could not be converted to string 这是我的代码: $result = mysqli_query
我是一名优秀的程序员,十分优秀!