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

id is required

Hey guys, when a try to give a Lists->create() in a controller my method is return that the field id is required but a set he this way:

CREATE OR REPLACE TABLE tl_list(
    id int unsigned not null auto_increment primary key,
    name varchar,
    ...
);
if($n_list->create() === false){
                    echo "Error: xxx";
                    $messages = $n_list->getMessages();

                    foreach ($messages as $message) {
                        echo $message, "\n";
                    }

It can be a problem of cache?

I'am using PHP 7.1 and Maria DB.

Perhaps you did not delete models meta data cache?



3.8k
edited Dec '17

I try debug with:

$lists = new Lists();
    $metadata = $lists->getModelsMetaData();
$attributes = $metadata->getAttributes($lists);
    print_r($attributes);
    $dataTypes = $metadata->getDataTypes($lists);
    print_r($dataTypes);

and:

Array ( [0] => id [1] => name [2] => url [3] => description [4] => id_subject [5] => list_type [6] => source [7] => allow_votes [8] => id_media [9] => id_user [10] => id_category ) 

Array ( [id] => 0 [name] => 2 [url] => 2 [description] => 2 [id_subject] => 0 [list_type] => 0 [source] => 2 [allow_votes] => 0 [id_media] => 0 [id_user] => 0 [id_category] => 0 )

and I try use reset() in meta data cache but nothings happens



93.7k
Accepted
answer
edited Dec '17

Can you show us how you set your models meta cache in your services?

Should look something like this:

$di->setShared('modelsMetadata', function() use ($config) {
    return new \Phalcon\Mvc\Model\MetaData\Files([
        'lifetime' => 86400,
        'metaDataDir' => $config->site->path->cache . 'models-metadata/'
    ]);
});


3.8k
edited Dec '17

I was not setting this, how was it working before? I was seeing this now and trying use Apc, and what this $config variable should be? is just for the path of the metaDataDir? Thanks.

--Edit again: I set the modelsMetadata as you sugest and continue returning the message that id is required. I use create in anothers models and works fine but in this specifically don't work but I can't find any difference in the code.



3.8k
edited Dec '17

I think that I found the error in my code, I was setting a foreign key of a relation 1:1 this way:

$tl_list->Media = $media; // I was trying object media and aparently this expect the id
$tl_list->id_media = $media->id; // and works 

GOod job solving it yourself! Sorry for not replying yesterday, fell asleep on the couch :)