Skip to content

Instantly share code, notes, and snippets.

@dogmatic69
Created November 20, 2009 13:15
Show Gist options
  • Save dogmatic69/239495 to your computer and use it in GitHub Desktop.
Save dogmatic69/239495 to your computer and use it in GitHub Desktop.
<?php
// the view to create csv's
$a = array_keys( ClassRegistry::init( Inflector::singularize( $this->name ) )->_schema );
$needed = array(
Inflector::singularize( $this->name ) => $a
);
$row = array();
if ( !empty( $data ) )
{
foreach( $needed[key( $needed )] as $head )
{
$parts[] = Inflector::humanize( str_replace( '_id', '', $head ) );
}
$row[] = implode( ',', $parts );
foreach( $data as $k => $array )
{
$parts = array();
foreach( $array[key( $needed )] as $field => $value )
{
if ( $field != 'id' )
{
if ( strpos( $field, '_id' ) && in_array( $field, $needed[key( $needed )] ) )
{
$parts[] = $array[Inflector::camelize( str_replace( '_id', '' , $field ) )][ClassRegistry::init( Inflector::camelize( str_replace( '_id', '' , $field ) ) )->displayField];
}
else if ( in_array( $field, $needed[key( $needed )] ) )
{
$parts[] = $value;
}
else
{
$parts[] = '';
}
}
}
$row[] = implode( ',', $parts );
unset( $parts );
}
}
$csv = implode( "\n", $row );
echo $csv;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment