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, Database and developer tools, case problems.

1.

My server's OS is linux, so table name is case sensitive, and this is my table

CREATE TABLE IF NOT EXISTS `Category` (
      `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `name` VARCHAR(50) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;

This is the class, the case of class name exactly same to table name

class Category extends \Phalcon\Mvc\Model
{
}

But when I use it.

$cate = new Category();
$cate->name="cate name";
$cate->save();

it will throw the exception.

PhalconException: Table "category" doesn't exist on database when dumping meta-data for Category

Why Phalcon will convert my class name to lower case?

I know I can use this to force the table name

public function getSource()
{
    return 'Category';
}

But the class name and table name is same, so this function should be optional.

Can I config the DI to prevent Phalcon to convert the case of class name?

2.

This is another table

CREATE TABLE IF NOT EXISTS `EmailActivation` (
      `code` VARCHAR(20) NOT NULL,
      `isValid` TINYINT(1) NOT NULL,
      PRIMARY KEY (`code`))
ENGINE = InnoDB;

I use Developer Tools to generate models for each table, and this is the generated model

class Emailactivation extends \Phalcon\Mvc\Model
{
        protected $code;
        protected $isValid;
        public function setCode($code)
        {
            $this->code = $code;

            return $this;
        }
        public function setIsvalid($isValid)
        {
            $this->isValid = $isValid;

            return $this;
        }
        public function getCode()
        {
            return $this->code;
        }
        public function getIsvalid()
        {
            return $this->isValid;
        }

}

This class also have incorrect class and function

e.g.

Emailactivation = X

EmailActivation = O

setIsvalid = X

setIsValid = O

getIsvalid = X

getIsValid = O

How to use the tool to generate correct case in class name and function?

edited Jul '14

Not 100% sure.. but I believe the table name has to be lowercase.

Might be able to add this in the model.

initialize() with $this->setSource("Category"); might work.

some discussion here https://github.com/phalcon/cphalcon/issues/356



98.9k
Accepted
answer

You can create a base model for your models implementing that feature:

class BaseModel extends \Phalcon\Mvc\Model
{
    public function getSource()
    {
        return get_class($this);
    }
}

Then use this class in your models:

class Category extends BaseModel
{

}