I have made this function:
https://gist.github.com/AndreiLN/3708ab829c26cee4711b1df551d1385f
/**
* Converte um objeto Doctrine para um array
* @param $dados
* @param $single define se é uma única execução (Sem recursividade)
* @return array
*/
public function doctrine_to_array($data, $single = false) {
if (is_object($data)) { // Verifica se é array ou objeto
$methods = get_class_methods($data);
$methods = array_filter($methods, function($val){ return preg_match('/^get/', $val); });
$return = [];
if(count($methods)){
foreach($methods as $method){
$prop = lcfirst(preg_replace('/^get/', "", $method));
$val = $data->$method();
if(!$single){
$return[$prop] = $this->doctrine_to_array($val, $single);
} else {
if(!is_array($val) && !is_object($val)){
$return[$prop] = $val;
}
}
}
}
return $return;
} else if(is_array($data)){
if(count($data)){
foreach($data as $idx => $val){
$data[$idx] = $this->doctrine_to_array($val, $single);
}
}
}
return $data; // Retorna o próprio valor se não for objeto
}
If you find some upgrade please let me know.
Explaining more of this function: it's get the doctrine object of array, if it's a object it's reads all get's methods to get all values, if this values is another doctrine object (And single option is not set) it's call the function recursively until it's done.
If the parameter is a array the function will go throught of it and call the method again for all it's values.
It's easy to use, but not tested in all situations.