SELECT COUNT(*)FROM' accounts'角色='工程师'使用symfony1.4获取总行数

时间:2014-04-03 14:57:48

标签: php mysql symfony-1.4

我在从表帐户获取所有数据时遇到问题,我想使用WHERE从表帐户中获取数据总数。让我们从表格中说出角色=工程师。字段role = engineer将获得的所有行总数。我在这个

上使用symfony框架

这是我的控制器

<?php
class spFindRoleDetailAction extends sfAction{

  function preExecute(){
  }

  public function execute($request){

        $this->setLayout('spLayout');
    $this->setTemplate('sp/roleDetail');

        $role = $request->getParameter('ro');
        $this->roleDetail = AccountTable::getInstance()->getRoleDetail($role);
        $this->roleCount = AccountTable::getInstance()->getCountDetail($role);

    }
}

模型查询

 public function getCountDetail($role){
                return $this->createQuery('a')
                ->where('a.role = ?', $role)
                ->execute();
     }

我的模块模板在这里查看代码

<div id="search-by-role-detail" data-role="page">
    <div role="main">
    <ul data-role="listview">  
      <li>
        <a href="<?php echo url_for('@find-sp-role-page'); ?>" class="ui-btn ui-btn-icon-left ui-icon-nav-l center"> 
          <h1>エンジニア<span class="header-result-count">(<?php echo $roleCount->count(); ?>)</span></h1>
        </a>
      </li>
            <?php if(isset($roleDetail)): ?>
            <?php foreach($roleDetail as $role): ?>
      <li>
        <a href="<?php echo $role->getSlug(); ?>"class="ui-btn ui-btn-icon-right ui-icon-list-r" data-transition="slide">
                    <?php if($role->getLogo() == NULL): ?>
                        <div class="middle v-image">
                            <img class="image-user" src="/images/sp/sample.png">
                        </div>
                    <?php else: ?>
                        <div class="middle v-image">
                            <img class="image-user" alt="<?php echo $role->getName(); ?>" src="http://img.creww.me/uploads/account/logos/<?php echo $role->getLogo(); ?>">
                        </div>
                    <?php endif; ?>
          <div class="middle v-list">
            <h3><?php echo $role->getName(); ?></h3>
            <p><?php echo $role->getOverview(); ?></p>
          </div>     
        </a>
      </li>
            <?php endforeach; ?>
            <?php endif; ?>
    </ul>
    </div>
    </div>

H1标签中有一个计数在那里,我将如何获得其中的总行数。我在那里使用<?php echo $roleCount->count(); ?>这是错误的

任何人都可以帮我解决这个问题? 任何帮助都非常感谢 谢谢!

1 个答案:

答案 0 :(得分:0)

如果count()无效,那么您可以尝试这样的事情。

public function getCountDetail($role)
{
   $q = Doctrine_Query::create()
        ->from('[some table] a')
        ->where('a.role = ?', $role)
        ->select('COUNT(a.id)')
        ->execute(array(),Doctrine_Core::HYDRATE_SINGLE_SCALAR)
   return $q;
}

在这种情况下,该函数将返回一个表示找到的记录数的数字。