简体   繁体   English

Select语句在Codeigniter的foreach循环中不返回任何内容

[英]Select statement returning nothing in foreach loop in Codeigniter

I have a foreach loop to loop through a decoded JSON object and run an INSERT on each loop, however I cannot get a SELECT statement to return anything within the same loop. 我有一个foreach循环来循环通过已解码的JSON对象,并在每个循环上运行INSERT,但是我无法获得SELECT语句来返回同一循环内的任何内容。

Here is my Model: 这是我的模特:

foreach (json_decode($detail, TRUE) as $key => $value)
    {
        $this->db->select('Type');
        $this->db->where(array('AssociationId' => $id, 'BeginDate' => $year, 'Account' => $value['account']));
        $query = $this->db->get('GLAccounts');

        $account = $query->row_array(); // <- nothing being returned here. Only one row should be returned

        if($account['Type'] == 'Asset' || $account['Type'] == 'Expense') // <- so this is being ignored
        {
            $value['debit'] = 1 * $value['debit'];
            $value['credit'] = -1 * $value['credit'];
        }

        else if($account['Type'] == 'Liablity' || $account['Type'] == 'Capital' || $account['Type'] == 'Income') // <- as is this
        {
            $value['debit'] = -1 * $value['debit'];
            $value['credit'] = 1 * $value['credit'];
        }

        $values = array(
            'id' => NULL,
            'JournalId' => $id,
            'Date' => $this->input->post('date'),
            'Account' => $value['account'],
            'Description' => $value['description'],
            'Debit' => $value['debit'],
            'Credit' => $value['credit']
        );

        $this->db->insert('GLJournalDetails', $values);

Is my structure wrong? 我的结构错了吗? I'f I run that EXACT same code that does the select('Type') using a direct controller->method call from a url and pass in static variables, I get a row_array back. 我运行了与从URL直接调用controller-> method调用select('Type')相同的代码,并传递了静态变量,我得到了row_array。 But it's not returning anything inside this loop. 但是它在此循环内未返回任何内容。 The INSERT works just fine. 插入工作正常。

尝试使用

$account = $query->row();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM