How to save doctrine database schema in a Symfony controller? - database

I'm using Symfony 2.3 and Doctrine 2 and i need that an user save the schema of a Doctrine Database to a file (*.sql). I need it into an action method and then send the file to the user

You need to execute following command:
.app/console doctrine:schema:create --dump-sql >schema.sql
And here's the answer how to run Command from Controller: How can I run symfony 2 run command from controller

Just to get you started, this should work in concept. I didn't get to run it, so I assume it might need a little tweaks from your side.
<?php
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
// your controller
public function myAction()
{
$command = new CreateSchemaDoctrineCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array('--dump-sql' => true));
$output = new NullOutput();
$schema = $command->run($input, $output); //This is your schema
// Write it to a file if you want
file_put_contents('path/to/schema.sql', $schema);
}
References:
Similar question
PHP file_put_contents()
Symfony2 FileSystem

Related

Html To PDF in CakePhp Using html2pdf Hangs

I use html2pdf, which is based on TCPDF, in CakePhp to render Views in PDF.
However, sometimes the generation hangs, I mean the browser freezes and never receives data.
There is a way to debug such a behavior? In apache logs I do not see any kind of error...
$this->set(compact('quotation','company','user'));
$view = new View(null, false);
$view->set(compact('quotation','company','user'));
$view->viewPath = 'Quotations';
$view->layout = 'preventivo';
if ($quotation['Quotation']['quotation_type'] == SERVICE)
{
$content = $view->render('print_s_template');
$this->set(compact('content'));
$this->response->type('pdf');
$this->render('print');
the print.ctp has
App::import('Vendor', 'HTML2PDF', array('file' => 'html2pdf'.DS.'html2pdf.class.php'));
$html2pdf = new HTML2PDF('P','A4','it');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
and the html is in print_s_template.ctp.
I found a solution myself. The problem is that I forgot to pass some variables to the View $view. And I suppose cake throw an error which, next, html2pdf cannot "render".
So: double check that all the variables in the view do exist!

Drupal: Running Feeds Importer Programatically - Where to Put Code

When running a feeds importer using Cron, the importer times out resulting in an incomplete import. I'm trying to use a script to execute the importer and I've come across this code a few times:
<?php
function MODULE_NAME_cron() {
$name = 'FEED_NAME';
$source = feeds_source($name);
$source->import();
}
?>
However, when executing this I get an error saying there's no feeds_source() function, which leads me to believe that I just don't know where to put this code (a separate php file just isn't working for me). Can anyone help me out here? Thanks in advance!
I think you need to call $source->startImport(); method instaed of $source->import();
I just posted an answer to a similar question over here: https://drupal.stackexchange.com/questions/90062/programmatically-execute-feed-import/153434#153434 , which might help. in short, in the a form submit hook if your using the batch api, or dont use the batch api if you're doing it non browser (install hook , install profile)
so in your case
function load_data(){
// Files to import in specific order.
$files = array(
'challenge_importer' => 'data/challenges.csv',
);
$file_location_base = drupal_get_path('module', 'challenge');
foreach ($files as $feed => $file) {
$feedSource = feeds_source($feed);
// Set the file name to import
$filename = $file_location_base.'/' . $file;
if (!file_destination($filename, FILE_EXISTS_ERROR)) {
$config = $feedSource->getConfig();
$config['FeedsFileFetcher']['source'] = $filename;
$feedSource->setConfig($config);
$feedSource->save();
while (FEEDS_BATCH_COMPLETE != $feedSource->import());
}
}
}
could be called from the cron , or use the scheduled execution from the feeds importer

PHPAGI USING CakePHP SHELL

I recently needed to build an asterisk based IVR for an application built using cakePHP.
I wanted to use cakes' (fat) models, so I wouldn't have to re-write business logic.
I wanted to create a cakePHP shell which would be called from the asterisk dialplan.
Here's what I did.
Downloaded phpagi to vendors/phpagi.
Modified phpagi.php from
function AGI($config=NULL, $optconfig=array())
to:
function AGI($config=NULL, $optconfig=array(), $stdin, $stdout)
so I can set stdin and stdout. Around line 167 I changed
$this->in = defined('STDIN') ? STDIN : fopen('php://stdin', 'r');
$this->out = defined('STDOUT') ? STDOUT : fopen('php://stdout', 'w');
To
$this->in = $stdin;
$this->out = $stdout;
In my shell in vendors/shells I added
App::import('Vendor', 'AGI', array('phpagi/phpagi.php'));
I also added
var $agi;
//redirect output through agi conlog
function err($message,$newlines = 1){
$this->agi->conlog($message);
}
function out($message, $newlines =1){
$this->agi->conlog($message);
}
//disable default message
function startup(){
}
and the application code is in
function main(){
$this->agi = new AGI(NULL, array(), $this->Dispatch->stdin,$this->Dispatch->stdout);
$this->agi->answer();
//do stuff here
}
and to run this in the dialplan all you need to do is
exten => s,n,AGI(${full/path/to/cake.php},${shellname},-app,${var/www/html/{appname}/app},-console,var/www/html/{appname}/cake/console/)
Make sure to fix permissions on the cake dirs.
I was wondering if anyone had any other ways to do this?
Here is a class that will handel all the work for you. It is maintained by FreePBX - the most popular Asterisk GUI, and used extensively throughout FreePBX.

CakePHP - spitting out XML for webservice

What is the best way to spit out XML for webservice in CakePHP?
I have it like the following but it's displaying an empty page.
Sample call /service/config.xml
In Controller
var $helpers = array('Xml');
function config() {
$this->autoRender = false;
$obj = array("response" => array("config" => array(...)));
$objXmlHelper = new XmlHelper();
$objXml = $objXmlHelper->header();
$objXml .= $objXmlHelper->serilize($obj);
echo $objXml;
}
That gives empty page. However, if I echo json_encode($obj); that actually prints out json.
Thanks,
Tee
You probably have an error in your code. My guess is you are not including the XML helper.
Check you CakePHP (app/tmp/logs/) and PHP logs. In addition you may need to set the DEBUG flag to a higher level ( i.e. > 0).
I'd also recommend considering moving such things to a model. Web Services are typically data access layers and that belongs in the Model.

use of upload class in codeigniter - Model or Controller?

Quick question about CI.
I have a view with a form, several text input fields and a file upload.
I want to be able to take the input from the text fields, save it to the DB, and then upload the image.
I've achieved this by having the upload code in a controller, and if the upload is successful, a call to my Model is made to update the database.
Is this "best practice", or indeed an acceptable way of doing it? Or should the File Upload go in the Model. Does it matter?
Essentially my code is:
function edit_category()
{
$config['upload_path'] = 'images/category/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '300';
$config['max_height'] = '300';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$this->session->set_flashdata('status', $this->upload->display_errors());
redirect('admin/category/edit/'.$this->input->post('catID'), 'location');
}
else /*no errors, upload is successful..*/
{
$fInfo = $this->upload->data();
//$this->_createThumbnail($fInfo['file_name']);
//process form POST data.
$data = array(
'catName' => $this->input->post('catName'),
'catDesc' => $this->input->post('catDesc'),
'catImage' => $fInfo['file_name']
);
/* update the database */
$category = $this->category_model->edit_category($data, $this->input->post('catID'));
I would put this in a model because I like to keep my controllers as slim as possible. I think of the controller as the link between the views and the back-room processing, not the processing itself.
I'm not sure if this is "best practise" or not. It will certainly work the way you're doing it too. CodeIgniter allows you to be quite flexible in how you apply mvc theory.
Use your models to interact with data whether it's a database interaction, an api call, or a file upload and download. Use your controller to run the show and make calls to that data. Do your best to keep them all separate in case the method for interacting with that data ever changes. Most of the time we think of the model as a database function, but it really should be ANY data no matter how it's retrieved.
I came out with this same dilemma, should I put the file upload functionality in controller or model.
After few trial and error I decided to put it under model for reusable purposes as calling controller from another controller is against the MVC concept.

Resources