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

Phalcon model is not recognizing AUTO_INCREMENT table column

The current table Structure is:

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title         | varchar(25)      | NO   | UNI | NULL    |                |
| content       | varchar(500)     | NO   |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

Model:

<?php

namespace Com\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Articles extends Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $title;

    /**
     *
     * @var string
     */
    public $content;

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'articles';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Articles[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Articles
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Validate if article titles are unique
     */
    public function validation()
    {
        $this->validate(new Uniqueness([
            "field" => "title",
            "message" => "Please select a different title."
        ]));

        return $this->validationHasFailed() != true;
    }

}

We are receiving the error: id is required

edited Jan '16

How do you call your model in controller?

It needs to be something like:

$articles = Articles::findFirstById($id);

if (!$articles) {
    $articles = new Articles();
}

// rest of the code

if you look after a row that doesn't exist, Phalcon doesn't create a new model instance by itself.

Thanks for the response, we are actually receiving the error when we are trying save data.

$article = new Articles();

$article->title = $this->request->getPost('title', 'striptags');
$article->content = $this->request->getPost('content');

if (!$article->save()) {
       $this->flash->error($article->getMessages());
} else {
       $this->flash->success("Article saved.");
       Tag::resetInput();
}

Any possible lead will be appreciated. :)

What is your DB engine?

What is your DB engine?

Thanks for your response. It is InnoDB.

Did you try to recreate your mysql table?

Also, does the raw SQL command fail?

INSERT INTO articles (title, content) VALUES ('foo','bar')

If it doesn't, I have no idea... :( It SHOULD work based on what you've posted....