Monday, November 1, 2010

Symfony 1.4 Making Proper Use Of MVC

Use of the Peer class in lib/model/


You can use this if you  need to get any datasets from the database
public static function ....

Use class in lib/model/


Put code here when you need to call functions from an object, even if that is linking to another object.

Use of Criteria


When using SQL statements, don't worry about converting dates and strings to work in SQL, Propel does it for you.

Clear cache


Clear cache when you think you need it, it does not matter when you are developing. Better to clear cache and get the latest changes than not.

Symfony link_to going to new window

From Symfony 1.x reference


<?php echo link_to('my article', 'article/read?title=Finance_in_France', array(
  'class'  => 'foobar',
  'target' => '_blank'
)) ?>

Thursday, October 14, 2010

Java Connecting to MySQL Database

Download MySQL Connector/J
You will need to download MySQL Connector/J from mySQL website under the deverloper's site. (http://dev.mysql.com last checked in October 2010).

Put it into a location that you can remember

Source Code
Need to import
import java.sql.*;

Code
Connection conn;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/dbschema", "username", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from table");

while (rs.next()) {
  ... [rs.getString(""), etc]
}
rs.close();
conn.close();

Monday, October 11, 2010

Ajax and Symfony

Installing jQuery
1. Go to http://www.jquery.com
2. Download the latest source and place in under web/js/

Include the javascript file as follows in the layout.php:
...

    <?php use_javascript('jquery-1.4.2.min.js') ?>
    <?php include_stylesheets() ?>
    <?php include_javascripts() ?>
...

Using jQuery
For using jQuery, refer to the jQuery website.

Here's a few examples

$('#status').text = 'loading';
.
.
.
<div id="status" name="status"></div>


$('#result').load('http://www.something here.com');
.
.
.
<div id="result" name="result"></div>

Thursday, October 7, 2010

Symfony 1.4 Automatically detect iPhone and use different pages

Note
This will make the whole site iphone aware, which means every single page must have an iphone page.

Changes in config/factories.yml
This will make Symfony know what to send when it is an iPhone format.

all:
  ...
  request:
    class: sfWebRequest
    param:
      formats:
        iphone: text/html

Changes in config/ProjectConfiguration.class.php
This will make Symfony look for the iPhone user-agent and change the format accordingly

public function setup()
  {
    ...
  
    $this->dispatcher->connect('request.filter_parameters', array($this, 'filterRequestParameters'));
  }

  public function filterRequestParameters(sfEvent $event, $parameters) {
    $request = $event->getSubject();
  
    $x = strpos($request->getHttpHeader('User-Agent'), 'Mobile');
    if ($x != '') {
      $x = '';
      $x = strpos($request->getHttpHeader('User-Agent'), 'Safari');
      if ($x != '') {
        $request->setRequestFormat('iphone');
      }
    }
    return $parameters;
  }

Symfony 1.4 Propel Create Schema From Database And Creating Model

Database Setup
To setup the database, change details in config/databases.yml and propel.ini

If you have multiple tables to generate, then you can create multiple database connections in databases.yml
and instead of
propel:
  class: sfPropelDatabase
  ...
change to
[connection name]:
  class: sfPropelDatabase
  ...

Create Schema From Database
There is a command called php symfony propel:build-schema to help create schema from database. You can refer this from 1.4 reference pdf.

After first running, the CLI has returned an error regarding date.timezone error. This is because in Mac, php.ini is not set by default. You can copy /etc/php.ini.default to /etc/php.ini to configure this according to your timezone.

CLI then returned [propel-schema-reverse] There was an error building XML from metadata: SQLSTATE[HY000] [2002] No such file or directory

This is because in databases.yml and propel.ini, the database host was set as localhost, which generates this error. Change this to 127.0.0.1

If you have duplicating table names, then change the phpName attribute in the schema files.

Creating Model From Schema
php symfony propel:build-model

php symfony propel:build-form
This will be used later.