phalcon-将结果集转换为数组时未定义关系? [英] phalcon - Relationships not defined when converting resultset to array?

查看:317
本文介绍了phalcon-将结果集转换为数组时未定义关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用2种方法进行了测试:

I have tested it with 2 methods:

第一个:

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;
    }
}

输出:

Employee: Admin

第二个:

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;
    }
}

输出:

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.

这样的事情:

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

推荐答案

是的,当您将结果集转换为数组时,仅转换标量值. 但是,要向模型中添加计算出的属性,无需将其转换为数组,您可以根据需要更改或创建新属性,并且仅在调用例如$project->save()时,它将保存到数据库中,而只是将其保存到数据库中.匹配的列名将存储在数据库中.

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.

对于添加计算的属性,我建议您使用事件afterFetch,该事件对于从数据库中检索到的每个模型都会触发:

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);
    }
}

这篇关于phalcon-将结果集转换为数组时未定义关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆