Zend: Select Object: How to replace the selected columns defined with ()?

First of all, what I'm trying to do:

In one of my classes in the library, I want to count the total number of rows of a search result. The class uses the selection object specified by the application of the search result model. My problem now, this select () already specified the requested columns with (), but just counted the rows that I just want to select id because the website needs to be executed. I cannot just change the values โ€‹โ€‹of an object because I use it in the library and the variables are protected. Unfortunately, Zend does not have a function for the count mySql command, and I do not want to use the static mySql code, because it may be that we will switch our database system in the future.

Now here is my question:

Is there a Zend_Select feature, how can I change the selected columns?

+7
source share
4 answers

Try the following:

$select->reset(Zend_Db_Select::COLUMNS) ->from('thetable', 'COUNT(*)'); 

replacing the "table" with the correct table name.

+15
source

This is from the project and not verified, but one of them should work.

 $select->from(array("table_name" => "table_name"), array("my_col" => "COUNT(id)")); 

OR

 $select->from(array("table_name"), array("my_col" => "COUNT(id)")); 

This is the same as

 SELECT COUNT(id) as my_col FROM table_name 

Hope that helps

Jake

+1
source

This one did not work for me (I needed to select from only one joined table):

 $select->reset(Zend_Db_Select::COLUMNS) ->from('thetable', 'COUNT(*)'); 
Perhaps because I had several associations. But, nevertheless, here is the solution: use reset () and then columns ():
 $select->setIntegrityCheck(false) ->from(['t1' => 'table1']) ->join(['t2' => 't2'], 't1.id = t2.t1_id') ->reset(Zend_Db_Select::COLUMNS) ->columns('t1.*'); 

Just FYI, Zend Framework version - 1.12

+1
source

To use the mysql command in a select element, you need to use Zend_Db_Expr:

 $select = $this->select() ->from('myTable', new Zend_Db_Expr('COUNT(id) as count')); echo $select; //SELECT COUNT(id) as count FROM myTable; 
-one
source

All Articles