2009-08-04 58 views
2

如果我想这样做(注意期满线):如何通过Doctrine将字面参数发送到数据库?

$duration=24; //in hours 

$reset=new PasswordReset(); 
$reset->code=md5(uniqid()); 
$reset->expires="now() + interval $duration hours"; 
$reset->User=$user; 
$reset->save(); 

我怎样才能教到其发送到PostgreSQL之前,请不要引用呢?我知道我可以在PHP中做到这一点,但我想知道以供将来参考。

回答

1

你可能想看看Using Expression Values

给出的例子是这样的(引述DOC)

$user = new User(); 
$user->username = 'jwage'; 
$user->updated_at = new Doctrine_Expression('NOW()'); 
$user->save(); 

,并生成该SQL查询:

INSERT INTO user (username, updated_at_) VALUES ('jwage', NOW()) 

我假设它会这样做,在你的情况?
(我没有启用学说项目就在这里,所以不能测试你)


一点题外话:我没有PostgreSQL服务器上测试;但是你提出关于MySQL不工作是什么:看来你必须使用“now() + interval 2 hour”,而不是“now() + interval 2 hours” - 不过,我不知道PG评论后


编辑

呃,你说得对,这不是一个正确的解决办法;这是行不通的:-(

嗯......有趣的问题,所以我挖多一点,就去主义的源代码,我想我可能发现一些有趣的事情

如果。你看看Doctrine_Query_Where::parseValue源的来源,你会发现这个代码部分:

// If custom sql for custom subquery 
// You can specify SQL: followed by any valid sql expression 
// FROM User u WHERE u.id = SQL:(select id from user where id = 1) 
} elseif (substr($trimmed, 0, 4) == 'SQL:') { 
    $rightExpr = '(' . substr($trimmed, 4) . ')'; 
// simple in expression found 

我已经是绝对没有试过,但是这可能是有趣......

也许你可以做这样的事情:

$reset->expires="SQL:(now() + interval $duration hours)"; 

如果你这样做,我很感兴趣,知道它是否会工作!
可能是有用的,一天或其他;-)

顺便说一句,似乎它在Doctrine_Search太习惯;看看这一个,也许它会工作没有括号,如下所示:

$reset->expires="SQL:now() + interval $duration hours"; 

嗯......我希望,这一次,它帮助...因为我看不出有其他办法可以做到你想之后的第二(第三获取(和谷歌并不能帮助我要么^^)


编辑什么,如果算上矿)评论。
我会得到这个工作......不然,我就没有睡好^^

好吧,我可能已经找到了一种方法(和,这一次,我测试了^^);不是真的一样大,一想,但是,反正更新时,它似乎是工作...

比方说,我有一个表创建这样:

CREATE TABLE `test1`.`test` (
    `id` int(11) NOT NULL auto_increment, 
    `name` varchar(32) NOT NULL, 
    `value` varchar(128) NOT NULL, 
    `date_field` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

相应的模型类是什么样子这样的:
也许并不完美:它是一个测试类,我别的东西写的,我已经突变,以适应这一^^

class Test extends Doctrine_Record 
{ 
    public function setTableDefinition() 
    { 
     $this->setTableName('test'); 
     $this->hasColumn('id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'unsigned' => 0, 
      'primary' => true, 
      'autoincrement' => true, 
      )); 
     $this->hasColumn('name', 'string', 32, array(
      'type' => 'string', 
      'length' => 32, 
      'fixed' => false, 
      'notnull' => true, 
      )); 
     $this->hasColumn('value', 'string', 128, array(
      'type' => 'string', 
      'length' => 128, 
      'fixed' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('date_field', 'integer', 4, array(
      'type' => 'timestamp', 
      'notnull' => true, 
      )); 
    } 
} 

我将插入两行到该表:

$test = new Test(); 
$test->name = 'Test 1'; 
$test->value = 'My Value 1'; 
$test->date_field = "2009-01-30 08:30:00"; 
$test->save(); 

$test = new Test(); 
$test->name = 'Test 2'; 
$test->value = 'My Value 2'; 
$test->date_field = "2009-02-05 08:30:00"; 
$test->save(); 

这让我从SQL这样的数据:
BTW:我没有PG服务器,所以我将测试与MySQL一切 - 应该在PG工作过,还是......

mysql> select * from test; 
+----+--------+------------+---------------------+ 
| id | name | value  | date_field   | 
+----+--------+------------+---------------------+ 
| 1 | Test 1 | My Value 1 | 2009-01-30 08:30:00 | 
| 2 | Test 2 | My Value 2 | 2009-02-05 08:30:00 | 
+----+--------+------------+---------------------+ 
2 rows in set (0.00 sec) 

因此,两条线,与日期很久以前在过去。


现在,可以肯定,我们只取线#1:

$testBefore = Doctrine::getTable('Test')->find(1); 
var_dump($testBefore->toArray()); 

我得到这样的输出:

array 
    'id' => string '1' (length=1) 
    'name' => string 'Test 1' (length=6) 
    'value' => string 'My Value 1' (length=10) 
    'date_field' => string '2009-01-30 08:30:00' (length=19) 


而且,目前,有趣的部分:让我们更新该行,使用类似于您提供的表达式来设置date_field值:

$query = new Doctrine_Query(); 

$query->update('test') 
    ->set('date_field', 'NOW() - interval 2 hour') 
    ->where('id = ?', 1) 
    ->execute(); 

var_dump($query->getSql()); 

的SQL,我得到的作为输出是这一个:

string 'UPDATE test SET date_field = NOW() - interval 2 hour WHERE id = ?' (length=65) 

这有点像你想要什么,如果我没有记错;-)


而且,只是可以肯定,让我们再一次抓取我们的产品线:

$testAfter = Doctrine::getTable('Test')->find(1); 
var_dump($testAfter->toArray()); 

我得到这样的结果:

array 
    'id' => string '1' (length=1) 
    'name' => string 'Test 1' (length=6) 
    'value' => string 'My Value 1' (length=10) 
    'date_field' => string '2009-08-04 21:26:30' (length=19) 

考虑的日期和时间,看来这个工作 - hoorray!

而且,可以肯定的,让我们直接从数据库中查询数据:

mysql> select * from test; 
+----+--------+------------+---------------------+ 
| id | name | value  | date_field   | 
+----+--------+------------+---------------------+ 
| 1 | Test 1 | My Value 1 | 2009-08-04 21:26:30 | 
| 2 | Test 2 | My Value 2 | 2009-02-05 08:30:00 | 
+----+--------+------------+---------------------+ 
2 rows in set (0.00 sec) 

而且...... Yeeeepe!


好了,现在,不那么好的部分:要能使用的语法,我不得不创建查询“手动”,与“漂亮”使用set()方法,而不是做模型类和save()方法:-(

现在,就由你来看看你,可以集成到你的模型类的...有与;-)

乐趣,如果你找到一种方法,有一天,要在查询的其他部分使用像这样的表达式,或者使用更简洁的方式,我非常感谢您是否可以发表评论以让我知道;-)


而且,这个时间,我衷心希望我找到的方式^^

+0

看起来Doctrine_Expression是使用SQL函数学说可以转换成便携的表达。在这种情况下它不起作用。 – ryeguy 2009-08-04 18:52:47

+0

Ergh :-(我用另一个想法编辑了我的答案......但是,如果这个也不起作用,恐怕没有多少希望:-( – 2009-08-04 19:14:32

+0

感谢您的挖掘,但它看起来像教义只有在where子句中才会查找“SQL:”。似乎并不在乎设置值时是否会这样做,尽管如此,我还是努力+1,我很欣赏它 – ryeguy 2009-08-04 20:48:41

相关问题