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

Get default value of a column (Model attribute)?

I'm trying to get the default value of the avatar of the User model so I may check if it's appropriate to delete the user's current avatar.

How would one get the default value 'd' of an attribute 'a' of a model 'm'?

I've tried using the modelsManager to execute a DESCRIBE modelName query but it returned nothing. I've also tried $def = $this->modelsManager->executeQuery("SELECT DEFAULT(avatar) FROM (SELECT 1) AS dummy LEFT JOIN User ON True LIMIT 1"); But that returns a Unexpected token near error.



2.1k

In your model:

$this->a = new \Phalcon\Db\RawValue('default');

?

That's for setting the model's value when inserting into the db. I actually had tested this by creating a test user Model and then setting the avatar attribute to the default RawValue and then printing it and the $user->avatar value was a RawValue, not the default for the avatar column.

In your model:

$this->a = new \Phalcon\Db\RawValue('default');

?

edited Jan '15

phalcon 2 in master branch:

$metaData = $robot->getModelsMetaData();

$values = $metaData->getDefaultValues($robot);
print_r($values);

$values - array of default values of fields for which exist default values

I'm using Phalcon 1.3.4. Is there a similar method to $metaData->getDefaultValues($robot);?, because that causes a 500 Internal Service Error

phalcon 2 in master branch:

$metaData = $robot->getModelsMetaData();

$values = $metaData->getDefaultValues($robot);
print_r($values);

$values - array of default values of fields for which exist default values


$result = $this->db->query("DESCRIBE yourtable");
$result->setFetchMode(\Phalcon\Db::FETCH_ASSOC);
while ($obj = $result->fetch()) {
    print_r($obj);
}