I'm trying to perform a selection with Propel which returns only calculated columns, but I allways have other columns selected.
For example:
$criteria = new MuestraQuery();
$criteria->clearSelectColumns()
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
Resulting query:
SELECT muestra.sal_id, muestra.sal_regimen,
-- ...
-- (ALL FIELDS OF THE TABLE HERE...)
-- ...
count(distinct muestra.sal_espesor) AS numEspesores
FROM muestra
I've been able to reduce the number of fields selected including only a field. For example, this query returns only two fields:
$criteria = new MuestraQuery();
$criteria->clearSelectColumns()
->select(MuestraTableMap::COL_SAL_ID)
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
Resulting query:
SELECT count(distinct muestra.sal_espesor) AS numEspesores,
muestra.sal_id AS "muestra.sal_id"
FROM muestra
¿Is there a way in Propel to select only computed columns?
I've seen columns are added in ModelCriteria->doSelect()
based on protected ModelCriteria->isSelfSelected
property, which is set in ModelCriteria->select()
but not in addAsColumn()
because it's from Criteria and it's not overriden in ModelCriteria.
Don't know if this is a bug or I'm doing something badly.
Just select the computed column that you added.
MuestraQuery::create()
->select(['numEspesores'])
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
->find();
Due to Propel's fluent api, you do not have to call the select
method prior to the addAsColumn
method, so you could even do the following:
MuestraQuery::create()
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
->select(['numEsesores'])
->find();