Friday, January 13, 2012

Jason Longshore: Symfony 1.4 and subdirectories for models, forms and filters

Jason Longshore: Symfony 1.4 and subdirectories for models, forms and filters

Symfony 1.4 with Doctrine stores all models in lib/model/doctrine, all forms in lib/form/doctrine and all filters in lib/filter/doctrine. This is a good behavior for small projects but can get out of hand when you have lots of tables as the folders get really large.

While there is an option "package" there are a few problems with its implementation. First, it is intended to be used for internal plugin implementation. As such, it introduces intermediate plugin classes even when you aren't packaging your models as a plugin. Second, it doesn't apply to form or filter classes.

Below is a patch to allow symfony to honor a new option, "directory" which will build these three items in sub-directories instead. As with all Doctrine schema options, you can apply this option in individual schema.yml files to all models or to only specific ones.

I just developed these mods this morning so there may be some issues with them. Ad-hoc testing seems to indicate that they work with at least the Doctrine features we use in our codebase : )

The patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Index: lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormGenerator.class.php
===================================================================
--- lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormGenerator.class.php (revision 6558)
+++ lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormGenerator.class.php (working copy)
@@ -93,6 +93,10 @@
$this->modelName = $model;
$baseDir = sfConfig::get('sf_lib_dir') . '/form/doctrine';
+
+ if($dirOption = $this->table->getOption('directory')) {
+ $baseDir .= '/'.$dirOption;
+ }
$isPluginModel = $this->isPluginModel($model);
if ($isPluginModel)
Index: lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormFilterGenerator.class.php
===================================================================
--- lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormFilterGenerator.class.php (revision 6558)
+++ lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormFilterGenerator.class.php (working copy)
@@ -76,6 +76,10 @@
$this->modelName = $model;
$baseDir = sfConfig::get('sf_lib_dir') . '/filter/doctrine';
+
+ if($dirOption = $this->table->getOption('directory')) {
+ $baseDir .= '/'.$dirOption;
+ }
$isPluginModel = $this->isPluginModel($model);
if ($isPluginModel)
Index: lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Import/Builder.php
===================================================================
--- lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Import/Builder.php (revision 6558)
+++ lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Import/Builder.php (working copy)
@@ -1189,7 +1189,13 @@
$definition['inheritance']['extends'] = $prefix . $definition['inheritance']['extends'];
}
}
-
+
+ $__path = $this->_path;
+
+ if(isset($definition['options']['directory'])) {
+ $this->_path = sprintf('%s/%s', $this->getTargetPath(), $definition['options']['directory']);
+ }
+
$definitionCode = $this->buildDefinition($definition);
if ($prefix) {
@@ -1199,7 +1205,7 @@
}
$fileName = $this->_getFileName($originalClassName, $definition);
-
+
$packagesPath = $this->_packagesPath ? $this->_packagesPath:$this->_path;
// If this is a main class that either extends from Base or Package class
@@ -1274,6 +1280,8 @@
} else {
$bytes = file_put_contents($writePath, $code);
}
+
+ $this->_path = $__path;
if (isset($bytes) && $bytes === false) {
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $writePath);

Applying to a specific model in a schema.yml file (Any .yml file in config/doctrine)

1
2
3
4
5
6
7
8
User:
options:
directory: prevention
tableName: xxx
columns:
..
UserAccount:
..

Applying to all models in a schema.yml file (Any .yml file in config/doctrine)

1
2
3
4
5
6
7
8
9
options:
directory: prevention
User:
tableName: xxx
columns:
..
UserAccount:
..
More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。

Thursday, December 15, 2011

flashfxp use private key

To use only private key in flashfxp

you need upgrade flashfxp to 4.0+

make sure connection type is sftp over ssh

in connection -> key manager -> import certificate

choose RSA/DSA key for SFTP

give a name of the certificate, can be anything

choose your private key file

enter your password if there's one

then save it

not finished yet!!!


after you save the certificate, make sure you selected it in your key manager, then you will be able to connect to your site via ssh

Tuesday, December 06, 2011

sfConfig in tasks to access app.yml

When you load a task, there is no application defined.

so you cannot get any app.yml definition.


for that you need to specify an application when you call the tasks ex:

symfony project:do-nothing --application="frontend"


and then you will be able to get the value of sfConfig::get('app_sender_email')

It should be possible to setup a default application, but i didn't find it out.

Friday, December 02, 2011

Functions to Import/Export CSV data utf-8

Documentation

CSVEport()

This function has just one argument - the query that returns the necessary data to be exported.

Example: CSVExport("SELECT name,username,email,url FROM User WHERE status=1");

CSVImport()

This will upload a CSV file and import the data into the specified table. This function must have the following arguments...

$table
The name of the table the data must be imported to
$fields
An array of fields that will be used
$csv_fieldname
The name of the CSV file field

Example: CSVImport("User", array('name','username','email','url'), "csv_file");

Code

<?php
//Get the result of the query as a CSV stream.
//http://www.bin-co.com/php/scripts/csv_import_export/
function CSVExport($query) {
    
$sql_csv = mysql_query($query) or die("Error: " . mysql_error()); //Replace this line with what is appropriate for your DB abstraction layer
    
    
header("Content-type:text/octect-stream");
    
header("Content-Disposition:attachment;filename=data.csv");
    while(
$row = mysql_fetch_row($sql_csv)) {
        print 
'"' . stripslashes(implode('","',$row)) . "\"\n";
    }
    exit;
}

//Import the contents of a CSV file after uploading it
//http://www.bin-co.com/php/scripts/csv_import_export/
//Aruguments : $table - The name of the table the data must be imported to
//                $fields - An array of fields that will be used
//                $csv_fieldname - The name of the CSV file field
function CSVImport($table, $fields, $csv_fieldname='csv') {
    if(!
$_FILES[$csv_fieldname]['name']) return;

    
$handle = fopen($_FILES[$csv_fieldname]['tmp_name'],'r');
    if(!
$handle) die('Cannot open uploaded file.');

    
$row_count = 0;
    
$sql_query = "INSERT INTO $table(". implode(',',$fields) .") VALUES(";

    
$rows = array();

    
//Read the file as csv
    
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        
$row_count++;
        foreach(
$data as $key=>$value) {
            
$data[$key] = "'" . addslashes($value) . "'";
        }
        
$rows[] = implode(",",$data);
    }
    
$sql_query .= implode("),(", $rows);
    
$sql_query .= ")";
    
fclose($handle);

    if(
count($rows)) { //If some recores  were found,
        //Replace these line with what is appropriate for your DB abstraction layer
        
mysql_query("TRUNCATE TABLE $table") or die("MySQL Error: " . mysql_error()); //Delete the existing records
        
mysql_query($sql_query) or die("MySQL Error: " . mysql_error()); // and insert the new ones.

        
print 'Successfully imported '.$row_count.' record(s)';
    } else {
        print 
'Cannot import data - no records found.';
    }
} 

if you have trouble to export as utf-8

you can try add BOM header