Symfony / Doctrine:$ this-> getRoute() - > getObject问题,只检索了一些记录

时间:2011-02-06 05:12:59

标签: symfony1 doctrine symfony-1.4

发表:

id   name   content
 1   test1  generated from fixture
 2   test2  generated
 3   test3  generated
 4   post1  this is actual post
 5   post2  real

路由:

post_show:
  url: /:name
  class: sfDoctrineRoute
  options: { model: Posts, type: object, allow_empty: true}
  param: { module: post, action: show, name:test1}

在操作中使用$this->getRoute()->getObject();将返回对象,例如/ example1,example.com / test2,example.com / test3,将不会返回所有其他查询(例如example.com/post1)。可能导致这种情况的原因是什么?

*我相信返回的记录(测试记录)和不记录的记录( post )之间的唯一区别是测试记录来自我的夹具

1 个答案:

答案 0 :(得分:1)

看看我的例子:

的routing.yml

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)

的actions.class.php

public function executeList(sfWebRequest $request) {
  $this->books = BookTable::getActiveWithAuthor($request->getGetParameters());
}

BookTable.class.php

public static function getActiveWithAuthor(array $parameters) {
  $bookQuery = BookQuery::create()
  ->addSelf()
  ->addAuthor();

  if(isset($parameters['date_from']))  
    $bookQuery->andWhere('b.updated_at > ?', $parameters['date_from']);
  return $bookQuery->execute();
}

这只是示例,但它显示了它的工作原理。 使用type:list,method:yourQueryMethod等。