ini_set("display_errors","1");
require_once('path-to/Doctrine-0.10.3/Doctrine-0.10.3/lib/Doctrine.php');
Doctrine::debug(true);
spl_autoload_register(array('Doctrine', 'autoload'));
Doctrine_Manager::getInstance()->setAttribute('model_loading', 'conservative'); // 'conservative' 'aggressive'
Doctrine::loadModels('news');
$dsn = 'mysql:dbname=my_test;host=127.0.0.1';
$user = 'my_test';
$password = 'my_test69';
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$conn = Doctrine_Manager::getInstance()->openConnection($dbh);
// insert
$c[0]='en_US';
$q = new Doctrine_Query();
$r = $q->from('SysLang sl')
    ->where('sl.id = ?')
    ->fetchArray($c);
$num=$q->count($c);
if(1>$num || null==$num){
    $sl = new SysLang();
    $sl->id=$c[0];
    $sl->active='1';
    $sl->updated=date(c);
    $sl->updater='Teixi';
    $sl->save();
}
// 03 Retrieve rows from SYS_LANG
$q = new Doctrine_Query ();
$result = $q ->from('SysLang sl')->fetchArray();
echo "<br />03 Results SYS_LANG:<br />";
foreach($result as $row) {
    foreach($row as $column) {
        echo $column." | ";
    }
    echo "<br />";
}
// 04 Insert language definitions in TRANS_LANG
// catalan
$c[0]='ca_ES';
$c[1]='ca_ES';
$q = new Doctrine_Query ();
$r = $q ->from('TranslLang tl')
    ->where(' tl.id_lang = ? AND tl.id_trad = ? ')
    ->fetchArray($c);
$num=$q->count($c);
if(1>$num || null==$num){
    $tl = new TranslLang();
    $tl->id_lang=$c[0];
    $tl->id_trad=$c[1];
    $tl->description='Català';
    $tl->save();
}
// 05 Retrieve rows from TRANS_LANG
$q = new Doctrine_Query ();
$result = $q ->from('TranslLang tl')->fetchArray();
echo "<br />05 Results TRANSL_LANG:<br />";
foreach($result as $row) {
    foreach($row as $column) {
        echo $column." | ";
    }
    echo "<br />";
}
// 06 Add a row in CONT_NEWS and each language content as three rows in TRANSL_NEWS
$c[0]='ca_ES';
$c[1]='1';
$c[2]='Primera Notícia de Prova';
$q = new Doctrine_Query ();
$r = $q ->from('TranslNews tl')
    ->where(' tl.id_lang = ? AND tl.id_news = ? AND tl.title = ? ')
    ->fetchArray($c);
$num=$q->count($c);
if(1>$num || null==$num){
    $cn = new ContNews();
    $cn->active='1';
    $cn->updated=$cn->created=date(c);
    $cn->updater=$cn->creator='Teixi';
    $cn->save();
    $id_n=$cn->get('id');
    // Insert Catalan
    $tn = new TranslNews();
    $tn->id_news=$id_n;
    $tn->id_lang='ca_ES';
    $tn->title='Primera Notícia de Prova';
    $tn->body='Aquest és el contingut de la Primera notícia de Prova';
    $tn->save();
    // Insert Spanish
    $tn = new TranslNews();
    $tn->id_news=$id_n;
    $tn->id_lang='es_ES';
    $tn->title='Primera Notícia de Prueba';
    $tn->body='Este es el contenido de la primera notícia de Prueba';
    $tn->save();
}
// 08 Test Delete  a row in TRANSL_NEWS
$q = new Doctrine_Query();
$d[0]='1';
$d[1]='en_US';
$r = $q->delete('tl.*')
                  ->from('TranslNews tl')
                  ->where('tl.id_news = ? AND tl.id_lang = ? ')
                  ->execute($d);
echo "<br />08 deleted=$r on TranslNews where id_news=$d[0] and id_lang=$d[1] <br />";
// retrieve view after delete
$q = new Doctrine_Query ();
$result = $q ->from('ViewNews vn')->fetchArray();
echo "<br />08 Results VIEW_NEWS:<br />";
foreach($result as $row) {
    foreach($row as $column) {
        echo $column." | ";
    }
    echo "<br />";
}
// 10 Update en_US
$u[0]='Updated: First News of Test ! '.date("s");
$u[1]='Updated: This is the content of the first news for testing ! '.date("s");
$u[2]='en_US';
$u[3]='1';
$k= new Doctrine_Query();
$k->update('TranslNews tl')
    ->set('tl.title', '?')
    ->set('tl.body', '?')
    ->where(' tl.id_lang = ? AND tl.id_news = ? ');
$z=$k->execute($u);
// 11 Retrieve Partial Data
$q = new Doctrine_Query ();
$result = $q ->from('ViewNews vn')->fetchArray();
echo "<br />11 Results VIEW_NEWS:<br />";
foreach($result as $row) {
    echo $row[title]." :: ".$row[body];
    echo "<br />";
}
// 12 Raw Sql
$q = new Doctrine_RawSql();
$q->select('{v.*}')
    ->from('VIEW_NEWS v')
    ->addComponent('v', 'ViewNews v');
$s=$q->getSql();
echo"<br />12 raw sql query= $s <br />";
$result = $q->execute();
echo "<br />12 Results Raw SQL: <br />";
foreach($result as $row) {
    foreach($row as $column) {
        echo $column." | ";
    }
    echo "<br />";
}