【问题标题】:Yii 1.* : how to get all relative table data as an arrayYii 1.*:如何将所有相关表数据作为数组获取
【发布时间】:2016-07-04 03:48:58
【问题描述】:

我的数据库查询:

$enqModel = EnquiryDetail::model(); $criteria = 新 CDbCriteria(); $criteria->together = true; $criteria->with = 数组( '课程提供', 'courseOffered.course' => 数组('alias'=>'coc'), '课程建议', 'courseSuggesteds.course' => 数组('alias'=>'csc'), '课程', 'courseTakens.course' => 数组('alias'=>'ctc'), 'courseOffered.course.stream' => 数组('alias'=>'cos'), 'courseSuggesteds.course.stream' => 数组('alias'=>'css'), 'courseTakens.course.stream' => 数组('alias'=>'cts'), '组织', '分支', '出席', '听说过', '模式', '注册人', '跟进', 'followups.followupsBy' => 数组('alias'=>'followups_by'), 'batchStudents', 'batchStudents.batch' => 数组('alias'=>'batch'), 'batchStudents.batch.batchDays' => 数组('alias'=>'batch_days'), 'paymentsPlans' => 数组('alias'=>'emiPlans') ); $result = $enqModel->findAllByAttributes(array('enquiry_id'=>$enqId),$criteria); 返回$结果;

现在我希望它全部作为 restApi 的数组返回,所以我将只传递 id,它会将所有相关数据作为数组返回。

问题是当我执行 findAll 或 findAllByAttributes 时会显示所有结果,但是当我尝试将其作为 CJSON::enode 传递时,只会显示父表数据。

【问题讨论】:

    标签: yii


    【解决方案1】:

    请你试试下面的代码:

    跟随 YII 框架文件的变化。

    文件路径:../yii/framework/db/ar/CActiveRecord.php

    CActiveRecord.php(添加代码行号:- 1871)

    /**
    * @return CMapIterator the iterator for the foreach statement
    */
    public function getIterator()
    {
           $attributes=$this->getAttributes();
           $relations = array();
    
           foreach ($this->relations() as $key => $related)
           {
                   if ($this->hasRelated($key))
                   {
                           $relations[$key] = $this->$key;
                   }
           }
    
           $all = array_merge($attributes, $relations);
    
           return new CMapIterator($all);
    }
    

    在您可以尝试您的代码并将所有相关表数据作为数组获取之后。

    【讨论】:

    • 感谢 Nikunj。它适用于 findByAttrubutes 但 findAll、findAllByAttributes 和另一件事框架中的任何更改都不是一个好主意,所以我在组件中创建一个类,然后将你的函数写入其中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2010-10-24
    • 2023-03-23
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多