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

Preventing "Describe <table>" database calls

Hi,

On every action, when I load a model and perform a query I see on mysql log a query "describe <table x>"; So if I load up 5 model, it executes 5 queries for "nothing".

I know that this is to map the db columns to the model but is there a way to cache this?

edited Aug '17
 Initialize the metadata for certain table
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initialize) {

    zephir_fcall_cache_entry *_2 = NULL;
    int ZEPHIR_LAST_CALL_STATUS;
    zval *model, *key, *table, *schema, *strategy = NULL, *className = NULL, *metaData = NULL, *data = NULL, *modelMetadata = NULL, *modelColumnMap = NULL, *dependencyInjector = NULL, *keyName = NULL, *prefixKey = NULL, *_3, *_0$$8, *_1$$8;

    ZEPHIR_MM_GROW();
    zephir_fetch_params(1, 4, 0, &model, &key, &table, &schema);

    ZEPHIR_INIT_VAR(strategy);
    ZVAL_NULL(strategy);
    ZEPHIR_INIT_VAR(className);
    zephir_get_class(className, model, 0 TSRMLS_CC);
    if (Z_TYPE_P(key) != IS_NULL) {
        ZEPHIR_OBS_VAR(metaData);
        zephir_read_property_this(&metaData, this_ptr, SL("_metaData"), PH_NOISY_CC);
        if (!(zephir_array_isset(metaData, key))) {
            ZEPHIR_INIT_VAR(prefixKey);
            ZEPHIR_CONCAT_SV(prefixKey, "meta-", key);
            ZEPHIR_CALL_METHOD(&data, this_ptr, "read", NULL, 0, prefixKey);
            zephir_check_call_status();
            if (Z_TYPE_P(data) != IS_NULL) {
                zephir_update_property_array(this_ptr, SL("_metaData"), key, data TSRMLS_CC);
            } else {
                if ((zephir_method_exists_ex(model, SS("metadata") TSRMLS_CC) == SUCCESS)) {
                    ZEPHIR_CALL_METHOD(&modelMetadata, model, "metadata", NULL, 0);
                    zephir_check_call_status();
                    if (Z_TYPE_P(modelMetadata) != IS_ARRAY) {
                        ZEPHIR_INIT_VAR(_0$$8);
                        object_init_ex(_0$$8, phalcon_mvc_model_exception_ce);
                        ZEPHIR_INIT_VAR(_1$$8);
                        ZEPHIR_CONCAT_SV(_1$$8, "Invalid meta-data for model ", className);
                        ZEPHIR_CALL_METHOD(NULL, _0$$8, "__construct", NULL, 9, _1$$8);
                        zephir_check_call_status();
                        zephir_throw_exception_debug(_0$$8, "phalcon/mvc/model/metadata.zep", 122 TSRMLS_CC);
                        ZEPHIR_MM_RESTORE();
                        return;
                    }
                } else {
                    ZEPHIR_OBS_VAR(dependencyInjector);
                    zephir_read_property_this(&dependencyInjector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
                    ZEPHIR_CALL_METHOD(&strategy, this_ptr, "getstrategy", &_2, 0);
                    zephir_check_call_status();
                    ZEPHIR_CALL_METHOD(&modelMetadata, strategy, "getmetadata", NULL, 0, model, dependencyInjector);
                    zephir_check_call_status();
                }
                zephir_update_property_array(this_ptr, SL("_metaData"), key, modelMetadata TSRMLS_CC);
                ZEPHIR_CALL_METHOD(NULL, this_ptr, "write", NULL, 0, prefixKey, modelMetadata);
                zephir_check_call_status();
            }
        }
    }
if u 实现了  the function meteData of oneModel, it will do。
else phalcon will call getMetaData from MYSQL by "describe `table`"

u can get help from doc Metadata Strategies https://docs.phalcon.io/en/3.2/db-models-metadata