データベースから販売者に関する情報を取得するクエリを作成しようとしていますが、そのストアが過去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フォーマットのタイムスタンプを返します。