Zend Framework Forum

Go Back   Zend Framework Forum > Zend Framework Forum > General Talks

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-2009, 03:29 PM
Junior Member
 
Join Date: Nov 2008
Location: Canada
Posts: 7
Default Zend_DB_Select using a SELECT string

Hi All

I would like to create a Zend_DB_Select object to
be used with Pagination

The Zend_DB_Select must be created from the sql below


SELECT member_id, name, email,
(select count(*) as downloads FROM download_table
where member_id = ?)
FROM member where member_id = ?

The list should look like this

member_id name email downloads
2 John jx@x.com 6
3 Jane jt@x.com 20
4 tom tt@x.com 10

How do I create a select object using a string
with the select query above?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-14-2009, 06:00 PM
SirAdrian's Avatar
Member
 
Join Date: Apr 2008
Posts: 83
Default

Try something like this.

[php]$fields = array(
'member' => array(
'member_id',
'name',
'email'
),
'download_table' => array(
'downloads' => 'count(download_table.downloadid)'
)
);

$select = $this->db->select()
->from('member', $fields['member'])
->joinLeftUsing('download_table', 'member_id', $fields['download_table'])
->where('member.member_id = ?', $id)
->group('member.member_id');[/php]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-15-2009, 08:15 PM
Junior Member
 
Join Date: Nov 2008
Location: Canada
Posts: 7
Default

Hi SirAdrian,

Thank you very much for your suggestion it worked as expected.

How do you account for a case where the sub-query is


select count(*) as downloads FROM download_table
where rating > 0)

OR

select count(*) as downloads FROM download_table
where rating > 4)

Where the download_table have fields
member_id, rating etc

In other words how do you account for
where clause in the subquery.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-15-2009, 08:36 PM
SirAdrian's Avatar
Member
 
Join Date: Apr 2008
Posts: 83
Default

You could try adding either: [php]->where('download_table.rating > ?', $minRating)[/php] or [php]->having('download_table.rating > ?', $minRating)[/php]

Seems to be more of an SQL issue. You do not need a subquery for this, just an aggregate query.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT. The time now is 12:27 PM.


Designed by: Miner Skinz Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0