phalcon - Relationships not defined when converting resultset to array?

醉酒当歌 提交于 2019-12-12 05:17:21

问题


I have tested it with 2 methods:

The first:

class ProjectsController extends ControllerBase
{
    public function indexAction()
    {
        $row = array();
        $projects = Projects::find();
        foreach ($projects as $project) {
            foreach($project->employees as $employee){
                echo "Employee: " . $employee->name;
            }
        }
        exit;
    }
}

Output:

Employee: Admin

The second:

class ProjectsController extends ControllerBase
{
    public function indexAction()
    {
        $row = array();
        $projects = Projects::find();
        $projects = $projects->toArray();
        foreach ($projects as $project) {
            foreach($project["employees"] as $employee){
                echo $employee->name;
            }
        }
        exit;
    }
}

Output:

Notice: Undefined index: employees in app/controllers/ProjectsController.php on line 10

When converting the resultset to array the relationships aren't added to the array, is there a workaround to add it to the array?

The reason I converted the resultset to an array is to edit results for example calculating progress or something like that, without saving it to the database.

Things like this:

foreach($projects as &$project){
    //count all the todos.
    $todos = Todos::find("project_id = '".$project["id"]."'");
    $numberOfTodos = $todos->count();

    //count all the todos that are done.
    $todos = Todos::find("project_id = '".$project["id"]."' AND status_id = 9");
    $numberOfDoneTodos = $todos->count();

    $project["percentageDone"] = ($numberOfDoneTodos / $numberOfTodos) * 100;
    var_dump($row);exit;
}
$this->view->setVar("projects",$projects);

So I don't have to do calculations on the view side and only have to output it


回答1:


Yes, when you convert a result set to an array only scalar values are converted. But for adding a calculated property to your model there's no need to convert it to an array, you can change or create new properties as you wish and it will only be saved to the database when you call for example $project->save() and just properties that match a column name will be stored in the database.

For adding calculated properties I'd recommend you to use the event afterFetch that gets fired for each model retrieved from the database:

class Projects extends \Phalcon\Mvc\Model
{
    ...
    public function afterFetch()
    {
        //Adds a calculated property when a project is retrieved from the database
        $totalTodos = Todos::count("project_id = $this->id");
        $completeTodos = Todos::count("project_id = $this->id AND status_id = 9");
        $this->percentageDone = round(($completeTodos / $totalTodos) * 100, 2);
    }
}


来源:https://stackoverflow.com/questions/23970211/phalcon-relationships-not-defined-when-converting-resultset-to-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!