Answer the question
In order to leave comments, you need to log in
Correct implementation of ActiveRecord in PHP in the manner of Rails?
Good afternoon!
I implement ActiveRecord in PHP, in the manner of a dozen analogues (Rails, Yii, and so on).
Let's say we have the following code that gets a list of objects:
$users = Users::model()->findAll()->all;
There was a question about the correctness of the implementation. The fact is that according to Wikipedia, “each instance of this class corresponds to one table entry;”. It seems to be ideologically correct to return an array of objects.
Yes, and for example the record
foreach ($users as $user){<br/>
print $user->name;<br/>
}<br/>
Answer the question
In order to leave comments, you need to log in
If we have 800 comments (or other entities) on the page, is it really possible to store 800 instances of objects somewhere? And ->all somewhere in the loop creates objects, binds them to a table and fills them with the received data?
UserCollection extends ArrayObject {
private $ids;
function __construct($where) {
$ids = db::query("SELECT id FROM Users WHERE $where")->getColumnValues('id');
}
public function offset($index) {
return new User(db::query("SELECT * FROM users WHERE id={$ids[$index]}"));
}
}
You can do things or classes:
table
class record
class table field
class select query
class select response class
Then we knead the whole thing and voila, there are no contradictions.
I recommend reading: "The Architecture of Enterprise Software Applications" by Martin Fowler - it describes in great detail about several approaches for obtaining records from the database.
Well, as a solution to your problem - when selecting many records, implement a wrapper for an array (a collection that is almost identical to a regular array), which will automatically receive new rows from the database as necessary (implementation is about an order of magnitude more complicated).
I usually do this. If I need to display many objects on the page, then I simply select from the database according to the necessary criteria and display the result. If I need to change an object or make a mass change of objects, then I select a list of idioms from the database, for each of them in a cycle I create an instance of the class and pull the necessary methods with subsequent saving.
> If we have 800 comments (or other entities) on the page, is it really possible to store 800 instances of objects somewhere?
I don’t remember about ActiveRecord, but in Mongoid in Rails there is a good example of how to overcome this. Until the special to_a method is called, any query like User.where(rights: 'admin').limit(200).offset(200) will return not an array of strings or objects, but an object of type MongoID::Criteria, which has a method each (similar to foreach) in such a way that it gradually takes line by line from the base and wraps it into an object. Thus, the garbage collector manages to hawp unnecessary objects quite quickly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question