toArray() function in Zikula_EntityAccess should also include relations #522

Open
gfr opened this Issue Oct 10, 2012 · 0 comments

Projects

None yet

2 participants

@gfr
gfr commented Oct 10, 2012

The function toArray() in Zikula_EntityAccess should also include relations.
The following code could be a base to add this feature but we have to add a parameter with the name of the calling entity to block endless loops.

    public function toArray()
    {
        $r = $this->getReflection();
        $array = array();


        while($r !== false) {
            $properties = $r->getProperties();
            $r = $r->getParentClass();

            foreach ($properties as $property) {
                if ($property->name == 'reflection' ) {
                    continue;
                }

                $method = "get" . ucfirst($property->name);
                $value = $this->$method();

                if($value instanceof Zikula_EntityAccess){
                    $value = $value->toArray();
                }else if ($value instanceof Doctrine\ORM\PersistentCollection){
                    if(!empty($value)){
                        $tmp = array();
                        foreach ($value as $key => $value) {
                            $tmp[] = $value->toArray();
                        }
                        $value = $tmp;
                    }
                }

                $array[$property->name] = $value;
            }
        }
        return $array;
    }
@Guite Guite modified the milestone: 2.0.0-beta1, 2.0.0 Nov 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment