我想有一个简单的方法,可以将
PHP Activerecord结果作为简单/关联数组,而不是ActiveRecord对象数组.
在Ruby中,我相信这可能是通过.map()方法完成的. (我不是ruby家伙…)
我想要的是一个简单的方法调用,比如Zend_DB_Table中的toArray(),而不是foreach,或类似的东西,但我似乎无法在他们的docs中找到它.
$settings = SystemSettings::all();
但它给出了这样的东西:
[0] => SystemSettings Object ( [errors] => [attributes:ActiveRecord\Model:private] => Array ( [param] => author [value] => Hawle ) [__dirty:ActiveRecord\Model:private] => Array ( ) [__readonly:ActiveRecord\Model:private] => [__relationships:ActiveRecord\Model:private] => Array ( ) [__new_record:ActiveRecord\Model:private] => ) [1] => SystemSettings Object ( [errors] => [attributes:ActiveRecord\Model:private] => Array ( [param] => base_url [value] => example.com ) [__dirty:ActiveRecord\Model:private] => Array ( ) [__readonly:ActiveRecord\Model:private] => [__relationships:ActiveRecord\Model:private] => Array ( ) [__new_record:ActiveRecord\Model:private] => )
虽然在很多情况下这真的很棒,但在这里,我只想拥有一个简单的数组,如下所示:
Array ( [author] => Hawle [base_url] => example.com )
我有一个类似的问题,希望这可以帮助其他人偶然发现它.显然,这是PHPactiverecord.org特有的.
public function to_array(array $options=array()) { return $this->serialize('array',$options); }
在/lib/Serialization.PHP中,我添加了以下类
class arraySerializer extends Serialization { public static $include_root = false; public function to_s() { return self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a(); } }
然后我可以调用 – > to_array()并返回一个数组.
希望这可以帮助!