有一个问题.
有我的路线方法:
book_list:
url: /api/books.:sf_format
class: sfDoctrineRoute
options: { model: Book, type: list, method: getActiveWithAuthor }
param: { module: book, action: list, sf_format: json }
requirements:
sf_format: (?:json|html)
行动中的代码很简单:
public function executeList(sfWebRequest $request) {
$this->books = $this->getRoute()->getObjects();
}
获取图书的自定义方法
public function getActiveWithAuthor(array $parameters) {
// crit is easy to find in logs.
sfContext::getInstance()->getLogger()->crit(var_export($parameters, true));
return BookQuery::create()
->addSelf()
->addAuthor()
->execute();
}
问题是,我想通过可选的参数“date_from”过滤书籍,这可能是在url中,例如/ API /书籍?date_from = 2011-02-18
但在日志中我只能看到“sf_format => html”(或json)
我应该使用什么来获取可选参数“date_from”?
最佳答案
public function executeList(sfWebRequest $request)
{
$this->books = Doctrine::getTable('Book')-> getActiveWithAuthor($request->getParameter('date'));
}
//BookTable.class.php
public function getActiveWithAuthor($date)
{
$q = $this->createQuery('b')
->leftJoin('b.Author')
->where('b.date > ?', $date);
return $q->execute();
}