Return all attributes in a model to an array

I’m currently running a gridview which exports to CSV.

The problem is I’m defining it like this. We are going to have custom variables grabbing names so the headers will change, so I’m trying to automate it to grab everything.


// $headers = array(

        //     'Forename',

        //     'Surname',

        //     'user.email',

        //     'user.telephone',

        //     'user.company',

        //     'user.company_role',

        //     'event.name',

        //     'status_id',

        //     'checkin_status_id',

        //     'Question1',

        //     'Question2',

        //     'Question3',

        //     'Question4',

        //     'Question5',

        //     'Question6',


        // );

$row = array();

        foreach($headers as $header) {

            $row[] = EventAttendees::model()->getAttributeLabel($header);

        }

        fputcsv($fp,$row);

And I was trying to automate it to grab everything, I tried this


$models = EventAttendees::model()->findAll();

        $headers = CHtml::listData($models);

        $row = array();

        foreach($headers as $header) {

            $row[] = EventAttendees::model()->getAttributeLabel($header);

        }

        fputcsv($fp,$row);

but it’s bringing up a 500 error.

Try your code to render in a view file, so you can trouble shoot using debug feature.

Also, define $fp.

$fp = fopen(‘php://temp’, ‘w’); is defined.

Got no more information regarding the error when viewing as a view.




$fp = fopen('php://temp', 'w');


foreach ($row as $r) {

    fputcsv($fp, $r);

}


fclose($fp);



my entire function is


 public function actionExport()

    {

        $fp = fopen('php://temp', 'w');

     

        /* 

         * Write a header of csv file

         */

        // $headers = array(

        //     'user.forename',

        //     'user.surname',

        //     'user.email',

        //     'user.telephone',

        //     'user.company',

        //     'user.company_role',

        //     'event.name',

        //     'status_id',

        //     'checkin_status_id',

        //     'Question1',

        //     'Question2',

        //     'Question3',

        //     'Question4',

        //     'Question5',

        //     'Question6',

            

        // );

        $headers = array(

            'Forename',

            'Surname',

            'Email Address',

            'Mobile Number',

            'checked in',

            'Question1',

            'Question2',

            'Question3',

            'Question4',

            'Question5',

            'Question6',

            

        );


        // $headers = CHtml::listData($modelHeader);

       

        $row = array();

        foreach($headers as $header) {

            $row[] = EventAttendees::model()->getAttributeLabel($header);

        }

        fputcsv($fp,$row);

     

        /*

         * Init dataProvider for first page

         */

        $model = new EventAttendees('exportSearch');

        $model->unsetAttributes();  // clear any default values

        if(isset($_GET['EventAttendees'])) {

            $model->attributes=$_GET['EventAttendees'];

        }

        $dp = $model->exportSearch($event_id);

        $dp->setPagination(false);

         

        /*

         * Get models, write to a file

         */

         

        $models = $dp->getData();

        foreach($models as $model) {

            $row = array();

            foreach($headers as $head) {

                $row[] = CHtml::value($model,$head);

            }

            fputcsv($fp,$row);

        }

     

        /*

         * save csv content to a Session

         */

        rewind($fp);

        Yii::app()->user->setState('export',stream_get_contents($fp));

        fclose($fp);

}

It grabs all rows, but falls over when grabbing the headers.

I think the array fields need to be given separately.

instead of




fputcsv($fp,$row);



try




foreach ($row as $r) {

    fputcsv($fp, $r);

}




or in your case




            foreach($headers as $head) {

                fputcsv($fp,CHtml::value($model,$head)); 

            }

            




I think I am wrong. I hope you get expert help from somebody else.

Since it only the problem with the headers in your csv, this might be a the solution. in CHtml, it is used in the following way.




        $row = array();

        $model = new EventAttendees();

        foreach($headers as $header) {

            $row[] = $model->getAttributeLabel($header);

        }

        fputcsv($fp,$row);

     




Sorry to end the previous post in a wrong note.

The problem iis with


$headers = array(

            'Forename',

            'Surname',

            'Email Address',

            'Mobile Number',

            'checked in',

            'Question1',

            'Question2',

            'Question3',

            'Question4',

            'Question5',

            'Question6',

            

        );

I can no longer define these above, so I’ve been triyn gto grab the attributes like so , but no luck


$headers = EventAttendees::model()->findall();

Why don’t you try lower case for all these items as we declare in the model?

It does work when it is declared such as this, but that is not what I want. As we have dynamic data we cannot add every field we have to controller function. What we need is a findall ()and what I’d assume is a chtml::listdata) to grab all the attributes. The trouble I’m having is that every time I’m grabbing all the attributes my CSV is coming back blank

There is another way to achieve that objective with a little tweaking of the below code.




        $headers = Yii::app()->db->createCommand('SHOW COLUMNS FROM `table_event_attendess`')->queryAll();

        $row = array();

        foreach($headers as $header) {

            $row[] = $header['Field'];

        }

        fputcsv($fp,$row);