0

我正在尝试学习 Symfony 文档书中的 DoctrineMongoDBBundle 教程。我已经创建了测试“产品”集合,并且能够毫无问题地插入其中,但是,我似乎无法从中读出。或者至少,我无法在我的视图中打印任何结果。

SymfonyProfiler 显示查询正在执行。但是,我的屏幕上没有显示任何内容。如果我不在视图中注释掉我的 foreach 循环,那么工具栏甚至不会出现。

控制器代码:

/**
* @route("/dbReadTest2/{id}")
* @Template()
*/
public function showAction()
{
    $repository = $this->get('doctrine_mongodb')
        ->getManager()
        ->getRepository('AcmeStoreBundle:Product');

    $products = $repository->findAll();

    return $this->render('AcmeStoreBundle:Default:index.html.twig', array(
        'products' => $products,
    ));
}

查看代码:

{% if products.count %}
  In if Block
  <ul>
  {% for product in products %}
    In For Loop
    <li>{{ product.name }} </li>
  {% endfor %}
  </ul>
{% else %}
    <p>. There are no products yet!</p>
{% endif %}
<p>End of Page</p>

加载后我得到的唯一输出是“In If Block”。没有其他评论出现。

谢谢!

4

2 回答 2

0

我以这种方式处理教义:

控制器:

$products = $this->getDoctrine()->getRepository("AcmeStoreBundle:Product")->findProducts();

产品库:

class GalleryRepository extends EntityRepository
{
     public function findProducts(){
          return $this->getEntityManager()->getRepository("TLWEntitiesBundle:Product")->findAll();
     }
}

其余的代码似乎没问题。

于 2013-07-12T00:57:46.703 回答
0

@user2566987

1) 绝对你在 twig 中访问传递的 $products 变量的代码是错误的。我没有足够的时间为您编写代码,但我可以指导您找到解决方案 2) 替换视图中的所有代码,{{ dump(products) }}然后重新加载页面,您将从那里看到所有数据,决定它们是 php 数据对象还是 php数据数组并使用适当的语法输出它们。3) 如果类的属性字段不存在,则您不能访问它们,例如count类产品的私有成员。我在链接教程http://symfony.com/doc/master/bundles/DoctrineMongoDBBundle/index.html中没有看到它

我希望它有帮助。

于 2016-01-12T18:34:55.647 回答