Archive for category PHP

how to build schema and keep I18n attributes in the yml file using propel and symfony 1.4

I had created I18n table set and all seemed to be working well until I tried to propel:build-schema.
I had lost my attribues:

isI18N: true, i18nTable: page_i18n & isCulture: true

This is how I want my schema to looked in the beginning:

  cms:
    _attributes: { phpName: Cms, isI18N: true, i18nTable: cms_i18n }
    id:
    handle: {type: varchar, size: 50}
    created_at:
    updated_at:
  cms_i18n:
    id:      { type: integer, size:  11, foreignTable: cms, foreignReference: id,  required: true, primaryKey: true }
    culture:     { type: varchar, size:   7, required: true, primaryKey: true, isCulture: true }
    content: { phpName: Content, type: LONGVARCHAR, required: true }

 

Basically how the build-schema work flow works like this

  1. Execute symfony propel: build-schema
  2. The sfPropelBuildSchemaTask script will create an schema.xml file from the database
  3. Then sfPropelBuildSchemaTask will create a schema.yml from the newly created schema.xml before deleting it

The way to fix this problem is to modify the schema.xml before it transformed into the schema.yml

This is how I fixed the problem.

  1. You need to find and modify the sfPropelBuildSchemaTask.class.php file
  2. Find:  protected function reverseDatabase()
  3. You will see where they define the yml and the xml path
            $xmlSchemaPath = sfConfig::get('sf_config_dir') . '/' . $name . '.xml';
            $ymlSchemaPath = sfConfig::get('sf_config_dir') . '/' . $name . '.yml';
  4. below this you should find an if block. There is no use modifying the xml if it doest exists
    if (file_exists($xmlSchemaPath)) {
  5. Immediately at the start of the ‘if’ block added this code, you may need to modify to fit your situation…
                $I18ntables = array();
                $schema = file_get_contents($xmlSchemaPath);
    
                $xml = simplexml_load_file($xmlSchemaPath);
    
                //iterate once to get all i18n tables
                foreach ($xml->table as $table) {
                    foreach ($table->attributes() as $attributeskey0 => $attributesvalue1) {
                        if ($attributeskey0 == 'name') {
                            if (preg_match("/_i18n/", $attributesvalue1)) {
                                $I18ntables[] = str_replace('_i18n', '', $attributesvalue1);
                            }
                        }
                    }
                }
                 //iterate again because we dont know the order of the tables
                 //grab the sister tables and add the new attributes
                foreach ($xml->table as $table) {
                    foreach ($table->attributes() as $attributeskey0 => $attributesvalue1) {
                        if ($attributeskey0 == 'name') {
                            if (in_array($attributesvalue1, $I18ntables)) {
                                $table->addAttribute('isI18N', "true");
                                $table->addAttribute('i18nTable', strtolower($attributesvalue1 . '_i18n'));
                            }
                            //as we are scrolling through the tables again, grab the i18n table
                            //find the column called 'culture' and add a new attribut to that
                            if (preg_match("/_i18n/", $attributesvalue1)) {
    
                                foreach ($table->children() as $columnkey0 => $columnvalue1) {
                                    if ($columnkey0 == 'column') {
                                        foreach ($columnvalue1->attributes() as $collAttrKey0 => $collAttrValue1) {
                                            if ($collAttrKey0 == 'name') {
                                                if ($collAttrValue1 == 'culture') {
    
                                                    $columnvalue1->addAttribute('isCulture', 'true');
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //the schema has changed now so assign it here
                $schema = $xml->asXML();

 

 

There are a couple of rules you need to know to use my code

  • the 2 I18N tables must be named the same , except for the ‘_i18n’ appendment of course
  • The column where you set the culture must be named ‘culture’

 

 

 

 

No Comments

Simple oAuth Twitter plugin for Symfony

This plugin was developed to allow you to post to ONE twitter account programmatically.

This plugin uses classes originally developed by Abraham Williams, TwitterOAuth handles the authentication of OAuth requests to interface with Twitter and can be used with all versions of Symfony.

* sf 1.0
* sf 1.1
* sf 1.2
* sf 1.3
* sf 1.4

Installation Method

download the plugin…

1. Copy the twitterOAuthPlugin dir to your plugins directory of your Symfony project

2. Activate the module ‘twitterOAuthPluginSetup’ in one of your symfony apps.

look at ‘Activating a plug-in Module’ in the book

3. clear cache

4. goto http://www.yourwebapp.com.au/twitterOAuthPluginSetup

5. you should get instructions similar to the steps below..

>>>

Steps:

1. Log into the Twitter account you want this symfony project to post to.

2. Go to http://dev.twitter.com/apps and ‘Register a new application’.

3. Give your application a unique name and description

4. for Application Website: enter, eg: http://www.yourapplication.com.au

5. Callback URL may be left blank. There’s no login process, so there’s no need to provide a URL to redirect users to.

6. Default Access type refers to the access your application will have to Twitter. Read & Write allows complete access. This is almost certainly what you want for your this project.

7. Replace the ‘xxx’ in the the plugin app.yml, twitterOAuthPlugin/config/app.yml, with your Consumer Key & the Consumer Secret.

twitter_consumer_key: xxx

twitter_consumer_secret: xxx

8. Go to ‘My Acesss token’

9. Replace the ‘xxx’ in the the plugin app.yml, twitterOAuthPlugin/config/app.yml, with your Access Token & the Access Token Secret.

twitter_access_token: xxx

twitter_ access_token_secret: xxx

10. Save the app.yml and clear cache.

11. To post a message to your Twitter account use the following static method in twitterOAuthPlugin.class.php

public static function sendTweet($message = null) { …..

eg: twitterOAuthPlugin::sendTweet(‘Test Message’);

No Comments

Integrating mediawiki with a symfony project symfony 1.3

I needed to add a mediawiki to a secure module in my symfony project

This is how I did it.

  • I downloaded mediawiki into my web dir web/wiki
  • After installation, I edited the LocalSettings.php, I added this block to secure the wiki.
    It is the same theory as a batch file:
require_once('../../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('backend', 'dev', true);
sfContext::createInstance($configuration);

$databaseManager = new sfDatabaseManager($configuration);
$databaseManager->loadConfiguration();

if(!sfContext::getInstance()->getUser()->isAuthenticated()) {
    sfContext::getInstance()->getController()->redirect("http://".$_SERVER['HTTP_HOST']."/user/login");
}

4 Comments

Specifying MySQL port on MediaWiki

>>>quoted from this site
http://microrants.blogspot.com/2007/08/specifying-mysql-port-on-mediawiki.html

MySQL listens on port 3306/tcp by default. MediaWiki’s 1.7.1 configuration is a bit misleading when it comes to specifying the database port. This excerpt is from LocalSettings.php:

$wgdBserver = “localhost”;
$wgDBname = “mw0″;
$wgDBuser = “my_user”;
$wgDBpassword = “my_pass”;
$wgDBprefix = “mw_”;
$wgDBtype = “mysql”;
$wgDBport = “3308″; // <— DOES NOT work for MySQL

The value in $wgBDport is only used with PostgreSQL. To specify a MySQL connection port different than 3306, use the syntax server:port on $wgdbServer. For instance:

$wgdBserver = “localhost:3308″;

BUT! there’s another catch: when the server is defined to “localhost”, MySQL will default to connecting via socket. If you are changing the port in hopes of getting WikiMedia to connect to another running instance of MySQL, chances are that it will still connect to the “default” one. To force a TCP connection use the loopback IP address instead of “localhost”:

$wgdBserver = “127.0.0.1:3308″;

No Comments

How to set an form error in the action. symfony 1.4

I have a field (company_abn).

I validate this field via web services in the action, so I need to set an error message in the action.

Here is how I did it..

            $register_parameters = $request->getParameter('user');
            $this->form->bind( $register_parameters );
            //do your abn validation stuff

            if(!$abn_valid){
                //set the value of the 'company_abn' field to invoke the 'required' error message
                $register_parameters['company_abn'] = '';
                //rebind the form to make it so that, isValid() will = false
                $this->form->bind( $register_parameters );
                //set your new error message for this widget
                $this->form->getValidator('company_abn')->setMessage('required', 'Your ABN is not Valid');

             }

2 Comments

The World of Programming

I found this image on another blog and liked it so much I uploaded it myself.

No Comments

How to change the format of the sfWidgetFormDateTime widget Symfony 1.4

This works for me….

$this->widgetSchema["the_date"]->setOption('date', array('format'=>' %day% %month% %year%'));

No Comments

Symfony Components

The Symfony Components are standalone and reusable PHP classes. With no pre-requisite, except for PHP, you can install them today, and start using them right away!

No Comments

add 1 day to a specific date in php

$from = date("Y-m-d",strtotime('+1 day', strtotime(date("$m/$d/$y"))));

No Comments

A way to Sort an array of Objects in php

I did get this from the PHP.net site..

http://au2.php.net/usort

name = $name;
    }

    /* This is the static comparing function: */
    static function cmp_obj($a, $b)
    {
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) {
            return 0;
        }
        return ($al > $bl) ? +1 : -1;
    }
}

$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");

usort($a, array("TestObj", "cmp_obj"));

foreach ($a as $item) {
    echo $item->name . "\n";
}
?>

The above example will output:

b
c
d

No Comments