1. <small id='xfZgP'></small><noframes id='xfZgP'>

          <bdo id='xfZgP'></bdo><ul id='xfZgP'></ul>

        <legend id='xfZgP'><style id='xfZgP'><dir id='xfZgP'><q id='xfZgP'></q></dir></style></legend>
      2. <tfoot id='xfZgP'></tfoot>
        <i id='xfZgP'><tr id='xfZgP'><dt id='xfZgP'><q id='xfZgP'><span id='xfZgP'><b id='xfZgP'><form id='xfZgP'><ins id='xfZgP'></ins><ul id='xfZgP'></ul><sub id='xfZgP'></sub></form><legend id='xfZgP'></legend><bdo id='xfZgP'><pre id='xfZgP'><center id='xfZgP'></center></pre></bdo></b><th id='xfZgP'></th></span></q></dt></tr></i><div id='xfZgP'><tfoot id='xfZgP'></tfoot><dl id='xfZgP'><fieldset id='xfZgP'></fieldset></dl></div>

        Symfony2 Doctrine 从一个类别中获取随机产品

        Symfony2 Doctrine get random product from a category(Symfony2 Doctrine 从一个类别中获取随机产品)
          <bdo id='fCexj'></bdo><ul id='fCexj'></ul>
        • <i id='fCexj'><tr id='fCexj'><dt id='fCexj'><q id='fCexj'><span id='fCexj'><b id='fCexj'><form id='fCexj'><ins id='fCexj'></ins><ul id='fCexj'></ul><sub id='fCexj'></sub></form><legend id='fCexj'></legend><bdo id='fCexj'><pre id='fCexj'><center id='fCexj'></center></pre></bdo></b><th id='fCexj'></th></span></q></dt></tr></i><div id='fCexj'><tfoot id='fCexj'></tfoot><dl id='fCexj'><fieldset id='fCexj'></fieldset></dl></div>

          <tfoot id='fCexj'></tfoot>

            <small id='fCexj'></small><noframes id='fCexj'>

              <tbody id='fCexj'></tbody>

            1. <legend id='fCexj'><style id='fCexj'><dir id='fCexj'><q id='fCexj'></q></dir></style></legend>
                • 本文介绍了Symfony2 Doctrine 从一个类别中获取随机产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下数据库方案:

                  table 'products'
                  id
                  category_id
                  

                  当然还有一个类别表,只有一个 id.

                  and of course a category table, just with an id.

                  数据看起来像这样:

                  Products
                  --------------------
                  | id | category_id |
                  --------------------
                  | 0  | 1           |
                  | 1  | 1           |
                  | 2  | 1           |
                  | 3  | 2           |
                  | 4  | 2           |
                  | 5  | 1           |
                  --------------------
                  

                  我想选择一个类别(例如类别 1),因此我在我的产品存储库类中选择该类别中的所有行:

                  I would like to select a category(for example Category 1), so I select all the rows from that category in my product-repository class:

                  return $this
                      ->createQueryBuilder('u')
                      ->andWhere('u.category = :category')
                      ->setMaxResults(1)
                      ->setParameter('category', $category->getId())
                      ->getQuery()
                      ->getSingleResult()
                  ;
                  

                  我现在如何选择随机产品?另外:是否可以通过关系解决这个问题?

                  How I can select now a random product? Also: Is it possible to solve this via Relationships?

                  我在实体Category"和Product"之间有一个 OneToMany 关系,所以我也可以通过 category->getProducts()...

                  I have a OneToMany Relationship between the entities "Category" and "Product", so I could also get all the products via category->getProducts()...

                  任何帮助都会非常有用,谢谢

                  Any help would be really useful, thanks

                  推荐答案

                  你首先要统计产品的总数,然后生成一个随机偏移量来选择一个随机产品.

                  You first have to count the total number of products, then generate a random offset to select a random product.

                  这应该让你开始:

                  $count = $this->createQueryBuilder('u')
                               ->select('COUNT(u)')
                               ->getQuery()
                               ->getSingleScalarResult();
                  

                  然后你可以在你的 1 和总行数之间生成一个随机数.

                  And then you can generate a random number between your 1 and the total number of rows.

                  return $this->createQueryBuilder('u')
                      ->where('u.category = :category')
                      ->setFirstResult(rand(0, $count - 1))
                      ->setMaxResults(1)
                      ->setParameter('category', $category->getId())
                      ->getQuery()
                      ->getSingleResult()
                  ;
                  

                  翻译为:

                  SELECT * FROM products WHERE category_id = ? LIMIT 1, {random offset}
                  

                  这篇关于Symfony2 Doctrine 从一个类别中获取随机产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='Pfg2I'></bdo><ul id='Pfg2I'></ul>

                      <tbody id='Pfg2I'></tbody>

                    1. <small id='Pfg2I'></small><noframes id='Pfg2I'>

                      <tfoot id='Pfg2I'></tfoot>
                      <i id='Pfg2I'><tr id='Pfg2I'><dt id='Pfg2I'><q id='Pfg2I'><span id='Pfg2I'><b id='Pfg2I'><form id='Pfg2I'><ins id='Pfg2I'></ins><ul id='Pfg2I'></ul><sub id='Pfg2I'></sub></form><legend id='Pfg2I'></legend><bdo id='Pfg2I'><pre id='Pfg2I'><center id='Pfg2I'></center></pre></bdo></b><th id='Pfg2I'></th></span></q></dt></tr></i><div id='Pfg2I'><tfoot id='Pfg2I'></tfoot><dl id='Pfg2I'><fieldset id='Pfg2I'></fieldset></dl></div>

                      1. <legend id='Pfg2I'><style id='Pfg2I'><dir id='Pfg2I'><q id='Pfg2I'></q></dir></style></legend>