Smarty: register_resource :db_get_timestamp

bosh

Новичок
Smarty: register_resource :db_get_timestamp

Берём код из мануала и децл его переписываем с целью учесть то, что у шаблона может быть несколько версий.
PHP:
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
	global $DB;
    // do database call here to fetch your template,
    // populating $tpl_source
    //$sql = new SQL;
    $sql=$DB->execute("select template
                   from templates
                  where template_name='$tpl_name' order by ctime desc limit 1",__FILE__,__LINE__);
    if ($DB->numRows($sql)) {
        $tpl_source = $DB->result($sql);
        return true;
    } else {
        return false;
    }
}

function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
    // do database call here to populate $tpl_timestamp.
    global $DB;
     $sql=$DB->execute("select ctime
                   from templates
                  where template_name='$tpl_name' order by ctime desc limit 1",__FILE__,__LINE__);
    if ($DB->numRows($sql)) {
        $tpl_timestamp = $DB->result($sql);
        return true;
    } else {
        return false;
    }
}

function db_get_secure($tpl_name, &$smarty_obj)
{
    // assume all templates are secure
    return true;
}

function db_get_trusted($tpl_name, &$smarty_obj)
{
    // not used for templates
}

// register the resource name "db"
$smarty->register_resource("db", array("db_get_template",
                                       "db_get_timestamp",
                                       "db_get_secure",
                                       "db_get_trusted"));

// using resource from php script
$smarty->display("db:index.tpl");
Это чудо шикарно заходит в БД, забирает оттуда шаблон и его парсит.
Проблема в том, что при внесении изменений в этот шаблон (добавлении новой записи в таблицу) - шаблон не перестаривается заново а берётся из templates_c

Возникла мысль что всему виной db_get_timestamp

Согласно мануалу:
The second function (в данном случае эта самая) is supposed to retrieve the last modification time of the requested resource (as a UNIX timestamp). The second parameter is a variable passed by reference where the timestamp should be stored. The function is supposed to return true if the timestamp could be succesfully determined, and false otherwise.
В моём случае эта функция выдает правильное время последнего изменения.
(2005-09-29 00:14:10)
Я предположил бы, что если это время отличается от времени прописанном в отпарсеном шаблоне, то должна вызываться функция
db_get_template
Однако она не вызывается и по-прежнему данные берутся из templates_c/%%84^840^84053873%%db%3Aindex.tpl.php

Внутри которого написано

PHP:
<?php /* Smarty version 2.6.7, created on 2005-09-29 00:12:39
         compiled from db:index.tpl */ ?>

Т.е. время уже "устаревшее" и по идее шаблон должен перегенерится.

Если шаблон физически мочкануть из templates_c то он успешно перегенерится новым контентом.

Собственно вопрос: я что-то не так делаю, или это баг?
 

Said

Guest
все нормально работает. если внимательно почитать мануал то там написано что функция должна возвращать время "as a UNIX timestamp" а не 2005-09-29 00:14:10
 

alexhemp

Новичок
bosh

Какой тип у поля "ctime"?

Сделай так: select UNIX_TIMESTAMP(ctime) from templates
 

bosh

Новичок
mysql> desc templates;
+---------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+-------------------+----------------+
| template_id | int(11) | | PRI | NULL | auto_increment |
| ctime | timestamp | YES | | CURRENT_TIMESTAMP | |
| template_name | varchar(250) | | | | |
| template | text | | | | |
+---------------+--------------+------+-----+-------------------+----------------+
4 rows in set (0.00 sec)

mysql> show create table templates;
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| templates | CREATE TABLE `templates` (
`template_id` int(11) NOT NULL auto_increment,
`ctime` timestamp NOT NULL default CURRENT_TIMESTAMP,
`template_name` varchar(250) NOT NULL default '',
`template` text NOT NULL,
PRIMARY KEY (`template_id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select ctime from templates;
+---------------------+
| ctime |
+---------------------+
| 2005-09-29 00:09:49 |
| 2005-09-29 00:09:49 |
| 2005-09-29 00:14:10 |
+---------------------+
3 rows in set (0.02 sec)
mysql> select unix_timestamp(ctime) from templates;
+-----------------------+
| unix_timestamp(ctime) |
+-----------------------+
| 1127938189 |
| 1127938189 |
| 1127938450 |
+-----------------------+
3 rows in set (0.00 sec)

Вообще говоря, я удивлен.

-~{}~ 29.09.05 17:11:

Хотя, я уже понял, что я из бронетанковых частей.
 
Сверху