活动记录问题返回数组(codeigniter)

时间:2013-11-24 19:55:36

标签: php mysql sql codeigniter activerecord

我的数据库中有一个名为 uploaded_files 的表格,其中包含以下字段:

  

id,file_name,upload_id,filetype_id

这最后一个字段(filetype_id)是一个名为 filetype 的表的外键,其中包含以下字段:

  

id,filetype_desc

我正在尝试在我的模型中创建一个方法,以返回表格 uploaded_files 中的所有字段,在此结构后面的数组中:

array(
            'file_type_1'  => array(
                'id'            => 1,
                'file_name' => "lalala.txt",
                'upload_id'     => $uploadId,
                'filetype_id'   => 1
            ),
            'file_type_2' => array(
                'id'            => 1,
                'file_name' => "lelele.txt",
                'upload_id'     => $uploadId,
                'filetype_id'   => 2
            ),
            'file_type_3' => array(
                'id'            => 1,
                'file_name' => "blabla.txt",
                'upload_id'     => $uploadId,
                'filetype_id'   => 3
            ),
    );
}

到现在为止,我来到了这里:

public function get_UploadedFilesFromUploadId($uploadId){
        //$query = "select * from uploaded_files where upload_id =".$uploadId;
        $query = $this->db->get_where('uploaded_files', array(
                                                            'upload_id' => $uploadId
                                                        )
                                    );
        return $this->db->query($query);
}

2 个答案:

答案 0 :(得分:1)

$query = $this->db->get_where('uploaded_files', array(
                                                            'upload_id' => $uploadId
                                                        )
                                    );
$res =  $this->db->query($query);

$result = array();
foreach ($res->result_array() as $row)
{
   $key = 'file_type_'.$row['filetype_id']; 
   $result[$key] = $row;
}

答案 1 :(得分:0)

$query = $this->db->get_where('uploaded_files',array('upload_id' => $uploadId));
foreach($query->result() as $row)
{
echo $row->filetype_id;
}

我认为这对你很有帮助。