Cphalcon: metadata->getReverseColumnMap and getColumnMap returning null

Created on 25 Jan 2018  路  4Comments  路  Source: phalcon/cphalcon

Expected and Actual Behavior

With version 3.0.1 at localhost, metadata->getReverseColumnMap works fine.
With version 3.2.4 at production server, metadata->getReverseColumnMap returns null

All queries, joins, etc are working, except this feature.

I'm using annotations strategy.

// configuring metadata
$di->setShared('modelsMetadata', function () {
    $metadata = new Phalcon\Mvc\Model\MetaData\Memory();
    $metadata->setStrategy(new Phalcon\Mvc\Model\MetaData\Strategy\Annotations());

    return $metadata;
});

And my models looks like:

namespace App\Models;

/**
 * @Source("moeda")
 */
class Moeda extends ModelBase {
    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false, column="codmoeda", nativeType="int")
     */
    protected $codmoeda;

    /**
     * @Column(type="string", nullable=false, column="nome", nativeType="varchar")
     */
    protected $nome;

    /**
     * @Column(type="string", nullable=true, column="icone", nativeType="varchar")
     */
    protected $icone;

    /**
     * @Column(type="string", nullable=true, column="chave", nativeType="varchar")
     */
    protected $chave;

// getters and setters

testing the code:

$model = new \App\Models\Moeda();
$metaData = $this->di->getShared('modelsMetadata');
$columnMap = $metaData->getReverseColumnMap($model);
var_dump($columnMap);

At localhost (3.0.1) prints the column names.
At production (3.2.4) prints NULL

Is there any special configuration that prevents the getReverseColumnMap from working?

EDIT

I just tested the annotations strategy separated and it gets the model metadata correctly on both servers:

$model = new \App\Models\Moeda();
$annon = new \Phalcon\Mvc\Model\MetaData\Strategy\Annotations();
$result = $annon->getMetaData($model, $this->di);
print_r($result);

but $annon->getColumnMaps return nothing on 3.2.4

$model = new \App\Models\Moeda();
$annon = new \Phalcon\Mvc\Model\MetaData\Strategy\Annotations();
$result = $annon->getColumnMaps($model, $this->di);
print_r($result);
not a bug

Most helpful comment

@hufersil, it's already cached in MetaData

Here is an easy way to get the columnMap:

$columnMap = $metaData->getColumnMap($model);

if ($columnMap === null) {
   $attributes = $metaData->getAttributes($model);

   $columnMap = array_combine(
      $attributes,
      $attributes
   );
}

This comes from cache as well, and in this case it will be also equal to the reverse column map.

All 4 comments

MetaData::getColumnMap() and MetaData::getReverseColumnMap() return null only if the names are identical, so there is nothing to map.

I am in favor of returning null as it's handled everywhere in the framework and it could prevent a lot of unnecessary loops.

what do you think about caching this information? Using the same strategy of metada? I think it鈥檚 better than returning null. Just a suggestion :-)

@hufersil, it's already cached in MetaData

Here is an easy way to get the columnMap:

$columnMap = $metaData->getColumnMap($model);

if ($columnMap === null) {
   $attributes = $metaData->getAttributes($model);

   $columnMap = array_combine(
      $attributes,
      $attributes
   );
}

This comes from cache as well, and in this case it will be also equal to the reverse column map.

Closing this because a solution was offered.

Was this page helpful?
0 / 5 - 0 ratings