Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:cakephp:cakephp_setup_and_configuration [2014/10/16 15:54]
smayr [TCPDF]
systems:cakephp:cakephp_setup_and_configuration [2016/05/16 14:18] (current)
smayr [Updating Model]
Line 15: Line 15:
 </code>     </code>    
  
 +Read a config variable using:
 +    Configure::read('Company.Name')
 == Database Configuration == == Database Configuration ==
 Edit file ''[cakephp]/app/Config/database.php'': Edit file ''[cakephp]/app/Config/database.php'':
Line 1272: Line 1274:
  
 echo $pdf->Output(APP . 'files/pdf' . DS . 'articles.pdf', 'I' /* Dest: F = File saved to disk, I = Inline to browser */);  echo $pdf->Output(APP . 'files/pdf' . DS . 'articles.pdf', 'I' /* Dest: F = File saved to disk, I = Inline to browser */); 
 +</code>
 +
 +=== PHPExcel ===
 +
 +  * Download a copy of PhpExcel helper and component for CakePHP 2.x: [[https://github.com/segy/PhpExcel]].
 +  * Copy files to respective folders in CakePHP app.
 +  * Get PHPExcel updates from [[https://github.com/PHPOffice/PHPExcel]], and update helper files in ''[app]/vendor/PhpExcel''.
 +  * See documentation:
 +    * [[https://github.com/segy/PhpExcel/blob/master/View/Helper/PhpExcelHelper.php|PhpExcelHelper]]
 +    * [[http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/05/07/4606.aspx|Use PHP to create Open XML Spreadsheet reports]].
 +
 +In PhpExcel helper, edit function ''createdWorksheet()'' to look like this:
 +<code php>
 +public function createWorksheet() {
 +    // load vendor classes
 +    App::import('Vendor', 'PHPExcel');  // <-- Check class name
 +
 +    $this->_xls = new PHPExcel();
 +    $this->_row = 1;
 +
 +    return $this;
 +}
 +</code>    
 +
 +In controller, add references to helper:
 +<code php>
 +class MyController extends AppController 
 +{
 +   ...
 +   public $helpers = array('Html', 'Form', 'Session', ..., 'PhpExcel');  
 +   ...
 +}   
 +</code>
 +
 +In controller, add action to generate data:
 +<code php>
 +...
 +function actionGetData($id)
 +{
 +    $data = ClassRegistry::init('Customer')->find('all', array(
 +                'conditions' => array('Customer.id' => $id),
 +            )); 
 +            
 +    $this->set('data', $data); 
 +        
 +    // Create a view that does not display any html header code except for the Excel data.
 +    // To do this, set the layout to be 'ajax', which just happens to contain no html outside the main content.
 +    $this->layout = 'ajax';
 +}    
 +</code>
 +
 +Create view. Eg: actiongetdata
 +<code php>
 +// create new empty worksheet and set default font
 +$this->PhpExcel->createWorksheet()->setDefaultFont('Calibri', 12);
 +
 +// define table cells
 +$table = array(
 +    array('label' => __('User'), 'filter' => true),
 +    array('label' => __('Type'), 'filter' => true),
 +    array('label' => __('Date')),
 +    array('label' => __('Description'), 'width' => 50, 'wrap' => true),
 +    array('label' => __('Modified'))
 +);
 +
 +// add heading with different font and bold text
 +$this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));
 +
 +// add data
 +foreach ($data as $d) {
 +    $this->PhpExcel->addTableRow(array(
 +        $d['User']['name'],
 +        $d['Type']['name'],
 +        $d['User']['date'],
 +        $d['User']['description'],
 +        $d['User']['modified']
 +    ));
 +}
 +
 +// close table 
 +$this->PhpExcel->addTableFooter();
 +    
 +// Save to Excel file
 +ob_end_clean();  // remove all output (very important, otherwise data is garbage)
 +this->PhpExcel->output('MyExcelFile.xlsx');
 +</code>
 +
 +== Delete Cached Models ==
 +
 +After updating a model, clear the cache to see the changes on the site. 
 +<code>
 +$ rm -f /app/tmp/cache/models/cake_model_*
 +$ rm -f /app/tmp/cache/persistent/cake_core_*
 </code> </code>