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

Model->Save() fails if field name is 'source'

Hi

In a table I have a column named 'source'.

Phalcon returns an error when I want to save a record in this table

my code:

                $params = array(
                    'ID'            => NULL,
                    'user_code'     => USER_CODE,
                    'source'        => $source,
                    'question'      => $question,
                    'true_answer'   => $trueAnswer,
                    'answers'       => $answers,
                    'date_added'    => $this->dateTime->getDate(),
                    'status'        => 'pending',
                    'score'         => 0
                );

                if ($sugg->save($params) == true)
                {

If I change the 'source' columns name everything get ok

sorry if my English is'nt very well ^-^



51.2k

"source" is a reserved word in Phalcon models. Change it.

Is there anyway to use this name, such as alias name?



51.2k
Accepted
answer

yes. You can use column mapping https://docs.phalcon.io/en/latest/reference/models.html#independent-column-mapping . But it's more easy and clean to just rename "source" in ... "question_source" for example.

Landing here because dealing with a legacy schema, changing the name is not a choice, will try the column mapping thing.