So far I've used many objects in my applications but often if I had to for example display for example users' profiles on page I simply got 20 users from database as array using some method in my object and assigned it to view.
到目前为止,我已经在我的应用程序中使用了很多对象,但是如果我不得不在页面上显示例如用户的配置文件,我只需要使用我的对象中的某个方法将数据库中的20个用户作为数组并将其分配给视图。
Now I want to create application more with models that represent real data. So for each user I should probably have User object with properties .
现在我想用表示真实数据的模型更多地创建应用程序。因此,对于每个用户,我应该具有带有属性的User对象。
Here I put sample code to get users from database and to display them in PHP:
在这里,我放置示例代码以从数据库中获取用户并在PHP中显示它们:
<?php
$db = new mysqli('localhost', 'root', '', 'mytest');
class User
{
private $id;
private $name;
public function __construct($data)
{
foreach ($data as $k => $v) {
if (property_exists($this, $k)) {
$this->$k = $v;
}
}
}
public function show()
{
return $this->id . ' ' . $this->name ;
}
}
$result = $db->query("SELECT * FROM `user` LIMIT 20");
$users = array();
while ($data = $result->fetch_object()) {
$data->x = 10; // just to test if property isn't created
$users[] = new User($data);
}
// displaying it on page
foreach ($users as $user) {
echo $user->show() . "<br />";
}
Questions:
-
Is it the way I should use data from database into model? I mean if should I create object for each record even if the only role of this object would be returning some data to view (for example even not modified by any functions as in this example). Of course I know that often data should be prepared to display or made some calculations or additional data should be retrieved from database before those data could be used to display.
这是我应该将数据库中的数据用于模型的方式吗?我的意思是,如果我应该为每个记录创建对象,即使该对象的唯一角色是返回一些数据进行查看(例如,即使没有像本例中的任何函数那样修改)。当然,我知道通常应该准备数据以显示或进行一些计算,或者在使用这些数据进行显示之前,应该从数据库中检索其他数据。
-
Is there any way to assign object properties from database simpler than using constructor with loop?
有没有办法从数据库中分配对象属性比使用带循环的构造函数更简单?
1 个解决方案
#1
0
First of all, i'd move the DB operations in a separate class, not inline with the User class. You could create an abstract Model class, which the User class would extend and add DB logic to it.
首先,我将DB操作移动到一个单独的类中,而不是与User类内联。您可以创建一个抽象的Model类,User类将扩展它并向其添加DB逻辑。
You'd have a select() method to query the database, which would return an array of objects of the class that extended the Model class (the User class in this case).
您将有一个select()方法来查询数据库,该方法将返回扩展Model类的类的对象数组(在本例中为User类)。
To answer your questions:
回答你的问题:
- I think it's ok. Most ORMs work this way.
- An alternative would be to assign the user row data from the DB to a $data attribute in your User class and use the magic methods __get and __set to access them.
我觉得没关系。大多数ORM以这种方式工作。
另一种方法是将用户行数据从DB分配给User类中的$ data属性,并使用魔术方法__get和__set来访问它们。
#1
0
First of all, i'd move the DB operations in a separate class, not inline with the User class. You could create an abstract Model class, which the User class would extend and add DB logic to it.
首先,我将DB操作移动到一个单独的类中,而不是与User类内联。您可以创建一个抽象的Model类,User类将扩展它并向其添加DB逻辑。
You'd have a select() method to query the database, which would return an array of objects of the class that extended the Model class (the User class in this case).
您将有一个select()方法来查询数据库,该方法将返回扩展Model类的类的对象数组(在本例中为User类)。
To answer your questions:
回答你的问题:
- I think it's ok. Most ORMs work this way.
- An alternative would be to assign the user row data from the DB to a $data attribute in your User class and use the magic methods __get and __set to access them.
我觉得没关系。大多数ORM以这种方式工作。
另一种方法是将用户行数据从DB分配给User类中的$ data属性,并使用魔术方法__get和__set来访问它们。