我在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();
怎么可能?
答案 0 :(得分:2)
由于方法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
答案 1 :(得分:0)
您可以在客户端模型中使用地图数组创建代理fromArray
,以便在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);
}