-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NFR] ORM. Ability to set custom Resultset class returned by find() #12166
Comments
Cool idea. |
Also able to make batch operations like change status. |
I think you can already do some batch things with resultset like: $resultset->update([
'status' => 2
]); Will update every row in resultset. |
@Jurigag thanks. this is enough for atomic operations but this can't change easily ongoing revisions if you have this snippet many place in big codebase. Change single function ease than multiple places. |
@tugrul well i guess there will be no problem at all to add method like |
namespace App\Models\Content;
class Category extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $parent;
public function getSource()
{
return 'content_categories';
}
public function initialize()
{
$this->hasMany('id', 'App\Models\Content\Category', 'parent', ['alias' => 'Childs']);
$this->belongsTo('parent', 'App\Models\Content\Category', 'id', ['alias' => 'Parent']);
}
}
namespace Resultsets;
class Categories extends \Phalcon\Mvc\Model\Resultset\Simple {
public function executeRecursive($callback, $level = 0) {
if (!is_callable($callback)) {
return;
}
foreach ($this as $category) {
$callback($category, $level);
$category->getChilds()->executeRecursive($callback, $level + 1);
}
}
}
echo '<select name="category">';
App\Models\Content\Category::find(['conditions' => 'parent IS NULL'])->executeRecursive(function($category, $level){
echo '<option value="', $category->id ,'">', str_repeat('– ', $level), $category->name, '</option>';
});
echo '</select>';
|
As a temporary solution to this, you are able to override the static find method on the model in question and force it to return the resultset you wish (return new Resultset('model', parent::find(...)); or something to that effect. I forget the exact logic I've used to acomplish this. It's hacky but gets the trick done. |
Implemented in |
@ashpumpkin Can this be revised to only go through the autoloader once -- instead of using class_implements() load the class instance and then check instanceof which will be faster as the autoloading() process only gets hit once. |
Hello. I think this feature is a good idea in cases when there`s some logic related to sets of models
Raugh but simple usecase example:
Day statistics table with columns (date, statistics_type, value). statistics_type can be 1,2 or 3.
You need to render a month graph with 3 types of stats on it, and inform user about min and max values
The view will take this data and render a page for current user. But the problem is that there are views and other objects where this data is fully or partially needed. And you need to always duplicate the code above or to seek for a place where this logic can be stored. I think Custom resultsets would be a good place for this.
Also it will help contributors to create their own Resultset classes for incubator.
The text was updated successfully, but these errors were encountered: