2

データベースから販売者に関する情報を取得するクエリを作成しようとしていますが、そのストアが過去3日間に開始された場合に限ります。クエリの日付を計算するために私が考えることができる最も簡単な方法は、new DateTime()オブジェクトを使用することです。コードを出力してテストすると、MySQLがクエリを実行するための適切な文字列になりますが、変数をバインドしようとするとエラーが発生します。Zend_Dbを使用してクエリを実行しています(PDOアダプター)

アクション:

public function indexAction()
{
    $dateMod = new DateTime();
    $dateMod->modify('-2 days');
    $dateMod->format('Y-m-d H:i:s');


    // get sellers initialized in last 3 days
    $sellerTable = new Application_Model_DbTable_Sellers();
    $select = $sellerTable->select()->setIntegrityCheck(false);
     $select->from(array('s' => 'seller'),array('sellerID', 'businessName'));
   // select firstName, lastName, picture from user table, and businessName and sellerID from seller table.  
   $select->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName', 'picture'));
   $select->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1 AND s.featured = 1');
   $select->where('s.launchDate > ?', $dateMod);
   $select->order('s.launchDate DESC');
   $newSellers = $sellerTable->fetchAll($select);

ビューに割り当てる$dateModと、正しいY-m-d H:i:s形式が出力されます。しかし、それをクエリにプラグインすると、次のエラーが発生します。

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY `b`.`launchDate` DESC' at line 2 

mysqlタイムスタンプ形式でdateModに値をハードコーディングすると、クエリは正常に機能します。DateTimeオブジェクトのタイムスタンプの文字列値だけにアクセスするにはどうすればよいですか? getTimestampフォーマットを割り当てた後でも、UNIXフォーマットのタイムスタンプを返します。

4

1 に答える 1

3

このformat()関数はフォーマットされた日付を返すため、クエリで使用する変数に日付を割り当てる必要があります。

$dateFormatted = $dateMod->format('Y-m-d H:i:s');

$select->where('s.launchDate > ?', $dateFormatted);
于 2012-05-07T21:45:25.063 に答える