0

I have an object named Property, how can I convert that to array

/**
 * @Route("/property/{id}/pictures/download_all", name="property_zip_files_and_download", methods={"GET"})     
 */
public function zipFilesAndDownloadAction(Property $property)
{
    $pictures = $property->pictures;
$compressPath = $this->get('some_service.property.picture_compress')->compress($pictures);
//some code for download...
....
}

How can I convert the pictures to array and pass it to my service? Can anyone please help me out here

1

2 Answers 2

0

What is pictures?

In simple cases you can use (array) $pictures.

Also, you can use Serializer Normalizers

if variable is Iterator (ArrayCollection or PersistentCollection for example) and service method has array as typehinting, you can convert it to simple array with iterator_to_array function.

Try:

$compressPath = $this->get('some_service.property.picture_compress')->compress(iterator_to_array($pictures));
Sign up to request clarification or add additional context in comments.

9 Comments

If I use (array) $pictures am getting an error in my service
Notice: Trying to get property of non-object inside my foreach $media = $picture->media;
What is $pictures? You're can show var_dump() result?
` /** * @ORM\OneToMany(targetEntity="My_Picture", mappedBy="my_property") */ public $pictures;`
Okay, you can use it as array, foeach be work. Also, you can convert it to simple array: $pictures = iterator_to_array($pictures);
|
0

This is how I turned my model MyObject object into a data array.

class MyObject{

    public function getContentArr($objInstance, $filter=true){

        $arr = array();

        if($objInstance){
            $response = new Response(GeneralFunctions::getKernel()->getContainer()->get('serializer')->serialize($objInstance, 'json'));

            $arr = json_decode($response->getContent(), true);

            //remove all items that are null, or empty string
            //if($filter){
               //$arr = $this->filterContentArr($arr); // optional
            //}
        }        

        return $arr;
    }

    /**
     * Returns filtered array removing empty/null
     * 
     * @param array $data
     * @return array
     */
    public function filterContentArr($data){               
        foreach($data as $key => $val){
            if(is_array($val)){

                $data[$key] = $this->filterContentArr($val);

                if(empty($data[$key])){
                    unset($data[$key]); //remove empty array
                }

            }else{
                if($val == "" || $val == null){
                    unset($data[$key]);
                }
            }
        }

        return $data;
    }
}

$myObject = new MyObject();
print_r($myObject->getContentArr($myObject));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.