We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Error: Column is not part of the column map.

Hello. I have a code that works ok with php 5.6 and Phalcon 2.0.x and php 5.6 and Phalcon 3, however when I have moved to php 7.0.11 and Phalcon 3.0.1 I started to get the error:

Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Column 'id' is not part of the column map in /myapp/apps/rest/controllers/ItemsController.php on line 32

A piece of code looks like this:

$invoice = Models\Invoice::findFirst($cartId);
$items = $invoice -> getInvoiceItems([
                                            'conditions'=>'part_number LIKE :part_n: AND part_description LIKE :part_descr: AND product_group LIKE :group:',
                                            'bind' => [
                                                        'part_n' => $getPartNum,
                                                        'part_descr' => $getPartDescription,
                                                        'group' => $getPartGrp
                                                      ],
                                             'order' => 'part_index'
                                           ]);

//this gives me error
$data = $items -> toArray();

If I dump $items ResultSet before toArray() I see:

protected '_columnMap' => 
    array (size=14)
      'id' => 
        array (size=2)
          0 => string 'id' (length=2)
          1 => int 0
      'invoice_id' => 
        array (size=2)
          0 => string 'invoice_id' (length=10)
          1 => int 0
// and so on...

As I understand it, this means that column id is actually in the column map ( and why it's important to have it anyway to convert to array?). What might cause this error then?

edited Sep '16

Can you give us the whole model code... or... just... i dont know. No idea. Maybe u have cache for models metadata/columns... if u set that try to clear it or disable all cache u have. But... maybe that is not the issue. I dont know, realy :/ Someone?

edited Sep '16

Hi, Boris.

Just downgraded to php 5.6.26 on the same machine. Yep, everything is working again. My best guess - this maybe caused by php not Phalcon. My model looks like this:

namespace CRM\Models;

use \CRM\Models\Part,
    \Phalcon\Validation,
    \Phalcon\Validation\Validator\PresenceOf,
    \Phalcon\Validation\Validator\StringLength,
    \Phalcon\Validation\Validator\Digit as DigitValidator,
    \Phalcon\Mvc\Model,        
    \Phalcon\Mvc\Model\Resultset\Simple as Resultset; 

class Item extends Model {     
    public $id;
    public $invoice_id;
    public $part_index;    
    public $part_id;
    public $part_number;   
    public $part_description;   
    public $price;
    public $qty;    
    public $points;
    public $discount;
    public $discount_special;
    public $product_group;
    public $start_date;
    public $end_date;

     public function initialize() {
        $this->setSource('item');
        $this->setSchema('CRM_data');
        $this -> belongsTo('invoice_id',  __NAMESPACE__ . '\Invoice', 'id',['alias' => 'ItemsInvoice']);
    }

    public function beforeValidationOnCreate() {
        //default values for points and discount if not set       
        $this->points = $this->points == null ? 0 : $this->points;
        $this->discount = $this->discount == null ? 0 : $this->discount;
        $this->discount_special = $this->discount_special == null ? 0 : $this->discount_special;
        $this->product_group = $this->product_group == null? '':$this->product_group;

        //if start date is set and end date is not setting end date = start date + 12 months
        $this->start_date = ($this->start_date == null)? null : $this->start_date;
        $this->end_date = ($this->end_date == null)? null : $this->end_date;
        if (!is_null($this -> start_date)) {            
            $s_date = new \DateTime($this -> start_date);
            $s_date -> modify( 'next year' );          
            $this->end_date = (is_null($this->end_date)) ? date($s_date->format('Y-m-d')):$this->end_date;                
        } else {
            $this->end_date = null;
        }               
    }

    public function beforeValidationOnUpdate() {             
       $this->beforeValidationOnCreate();       
    }

    //TODO: do smthing with this poor validation
    public function validation() {

        $validator = new Validation();
        //******Validation should be irrelevant to this problem I think

    }    
}

//******Other model's methods are irrelevant to this problem

You model looks great :/ so, maybe u are right. But still... u must figure out what is the issue if u upgrade someday. If u have time to chek with other phalcon version... - i dont know.

Good luck there!!!

edited Sep '16

You model looks great :/ so, maybe u are right. But still... u must figure out what is the issue if u upgrade someday. If u have time to chek with other phalcon version... - i dont know. Good luck there!!!

Thanks) I will check when I have enought time. For now I'm stick with php 5.6.



32.3k
Accepted
answer

Well I found a solution, when is a Simple Resultset is better use $result->toArray(false)

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/resultset/simple.zep#L153

edited Nov '16

Yep, that solves it! Have no idea why would anyone want rename columns as default toArray() behaviour, especially given that toArray() is slower when you rename these columns.

And now is time for php7-phalcon perfomance test!

Well I found a solution, when is a Simple Resultset is better use $result->toArray(false)

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/resultset/simple.zep#L153