0

我在 Propel 中的变量大小写有问题

当前有效的代码:

$this->_variables = array('Alias' => 'aliasOne', 'LocationId' => 1);
$model = new Client();
$model->fromArray($this->_variables);
$model->save();

但是由于我的 API 输出的格式,我希望代码是

$array = array('alias' => 'aliasOne', 'location_id' => 1);
$model = new Client();
$model->fromArray($array);
$model->save();

这怎么可能?

4

2 回答 2

2

由于方法的第二个参数,它已经由 Propel 处理fromArray()

$array = array('alias' => 'aliasOne', 'location_id' => 1);
$model = new Client();
$model->fromArray($array, BasePeer::TYPE_FIELDNAME);
$model->save();

在此处查看此常量的定义和其他常量:https ://github.com/propelorm/Propel/blob/master/runtime/lib/util/BasePeer.php#L63

于 2012-06-25T09:10:57.433 回答
0

您可以fromArray在 Client 模型中使用映射数组创建代理来转换您的密钥,在您的lib/model/om/BaseClient.php

public function myFromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
{
  $map = array(
    'alias'       => 'Alias',
    'location_id' => 'LocationId',
    // you can add more
  );

  $newArr = array();
  foreach ($arr as $key => $value)
  {
    // replace the key with the good one
    if (array_key_exists($key, $map))
    {
      $newArr[$map[$key]] = $value;
    }
    else
    {
      $newArr[$key] = $value;
    }
  }

  $this->fromArray($newArr, $keyType);
}
于 2012-06-22T12:11:10.887 回答