SELECT COUNT(*) FROM 'accounts' WHERE role = 'engineer' getting the total number of rows using symfony1.4

StackOverflow https://stackoverflow.com/questions/22841451

  •  27-06-2023
  •  | 
  •  

Question

Hi i have a problem in getting all the data from the table accounts, I want to get the total number of data's from the table accounts using WHERE. Let's say from the table accounts where role = engineer. all the total number of rows from the field role = engineer will get. Im using symfony framework on this

Here's my controller below

<?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);

    }
}

Model query

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

and my module templates view code here

<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>

The one in the H1 tag there is a count in there, How will i able to get the total number of rows in it. Im using there a <?php echo $roleCount->count(); ?> which is wrong

Anyone can help me out on this?? Any help is muchly appreciated Thanks!

Was it helpful?

Solution

If count() isn't working then you can try something like this.

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;
}

In this case the function will return a single number representing the number of records found.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top