【问题标题】:Phalcon - Custom Model MetaData strategyPhalcon - 自定义模型元数据策略
【发布时间】:2014-02-05 12:56:12
【问题描述】:

我想为 Phalcon 模型创建自己的自定义元数据解析器(因为当前没有索引注释并将一列映射到另一个参数)。不幸的是,当我将映射作为数组返回时:

/**
 * @param \Phalcon\Mvc\ModelInterface $model
 * @param \Phalcon\DiInterface $di
 * @return array
 */
public function getColumnMaps($model, $di)
{
    $columns = [];

    /** @var \Phalcon\Annotations\Reflection $reflection */
    $reflection = $di->get('annotations')->get($model);

    foreach ($reflection->getPropertiesAnnotations() as $name => $collection) {
        if ($collection->has('Column')) {
            $columns[$collection->get('Column')->getNamedArgument('column') ?: $name] = $name;
        }
    }

    return $columns;
}

我发现,该方法中的Phalcon\Mvc\Model\MetaData\Strategy\Annotations 返回null。我的结果类似于ID_IN_TABLE => PARAMETER_NAMEarray,例如。 'referer_id' => 'refererId' 将数据库中的字段与模型中的驼峰式字段映射为下划线。当我尝试使用findcountfindFirst 方法时,它会抛出错误Notice: Undefined index: 0Notice: Undefined index: 1,例如:

Users::find([
    implode(' AND ', $conditions),
    'bind' => $bind,
    'limit' => $this->getLimit(),
    'offset' => $this->getLimit() * ($this->getPage() - 1)
])

我做错了什么?

提前致谢, 大卫。

【问题讨论】:

    标签: php annotations models phalcon


    【解决方案1】:

    getColumnMaps() 的返回值应该是这样的:

    array(
        0 => $ordered_column_map,
        1 => $reverse_column_map
    );
    

    其中$ordered_column_mapID_IN_TABLE => PARAMETER_NAME amd $reverse_column_maparray_flip($ordered_column_map)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2011-03-31
      • 2011-08-23
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多