ZF2 TableGateway question

For programming and general questions on Zend Framework

ZF2 TableGateway question

Postby dcheli on Thu Nov 28, 2013 8:39 pm

Can anyone help me code up this simple SQL statement using TableGateway
SELECT COUNT(*) from mytable WHERE request = 2;

I'm getting hung up on the COUNT(*) clause
Here is what I've been trying to work with:

$resultSet = $this->tableGateway->select(function (Select $select) {
$select->columns(array('requestCount' => new Expression('Count (*)')));
$select->where(array('request; => 2));
} );

Thanks
Dave
dcheli
 
Posts: 17
Joined: Wed Jan 23, 2013 1:59 pm

Re: ZF2 TableGateway question

Postby dcheli on Thu Nov 28, 2013 10:00 pm

I found my mistake - the expression 'Count (*)' should be 'Count(*)' ; no space between Count and (

$select->columns(array('requestCount' => new Expression('Count(*)')));
dcheli
 
Posts: 17
Joined: Wed Jan 23, 2013 1:59 pm

Re: ZF2 TableGateway question

Postby sath2206 on Thu Feb 23, 2017 6:25 am

How to display it in index.phtml????
sath2206
 
Posts: 1
Joined: Thu Feb 23, 2017 6:24 am

Re: ZF2 TableGateway question

Postby mehm8471 on Tue Mar 07, 2017 4:44 pm

sath2206 wrote:How to display it in index.phtml????


If you really mean "index.phtml" that means it is likely a view template called by controller indexAction function. What you basically need is injecting your database mapper into the controller and use it to return the desired information to print on the view. This is what Album tutorial already covers.

However, (I might be wrong but worth to mention) I think you might be actually meaning to display the number in the "layout.phtml" as the number will be displayed no matter which controller is in charge by the requested route - like displaying total incoming messages as a badge on the "Messages" menu item displayed on navigation bar globally. In this case, I generally use the Application Module onBootstrap function to set a layout variable with the desired information and use it in the layout template like $layout->totalMessages.

Here is a sample for the second situation:
http://stackoverflow.com/questions/18439364/how-to-pass-variables-to-layout-phtml-globally-in-zf2

Note: I will also post a similar situation into my blog at Online Book Catalog Application Tutorial series soon but not at that step yet.

Suat
Suat
smozgur.com
mehm8471
 
Posts: 23
Joined: Sat Jun 21, 2014 11:39 pm


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 1 guest