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

skipAttributes - not using column map ?

Hello.

As the title suggests, i was creating a model but i had problems as i had a column (in the database) name login_date and at the columnMap() function it was being renamed to loginDate.

And on initialize() function it executed $this->skipAttributes(array("loginDate")); and it was not working but when i changed to $this->skipAttributes(array("login_date")); it worked.

Well, is this a bug or feature ? As everywhere else, when working with Phalcon Models, it uses the renamed column, not the original...

In our base Model we just overwrote the skip attributes methods

/**
 * Overwrite skipAttributes to support columnMap
 *
 * @param string[] $columns
 *
 * @return void
 */
public function skipAttributes(array $columns) {
    parent::skipAttributes($this->convertColumnsToRawFields($columns));
}

/**
 * Overwrite skipAttributes to support columnMap
 *
 * @param string[] $columns
 *
 * @return void
 */
public function skipAttributesOnUpdate(array $columns) {
    parent::skipAttributesOnUpdate($this->convertColumnsToRawFields($columns));
}

/**
 * Overwrite skipAttributes to support columnMap
 *
 * @param string[] $columns
 *
 * @return void
 */
public function skipAttributesOnCreate(array $columns) {
    parent::skipAttributesOnCreate($this->convertColumnsToRawFields($columns));
}

/**
 * Converts the columns into their original raw fields
 *
 * @param string[] $columns
 *
 * @return \string[]
 */
private function convertColumnsToRawFields(array $columns) {
    $columnMaps = $this->columnMap();
    foreach($columns as $key=>$value) {
        if(!array_key_exists($value, $columnMaps)) {
            //Look up in the changed values
            $index = array_search($value, $columnMaps);
            if($index===false) {
                throw new \InvalidArgumentException('Invalid Column, could not find: '.$value);
            } else {
                $columns[$key] = $index;
            }
        }
    }
    return $columns;
}