Buenas de nuevo,
He añadido en el modelo la variable $cantidad a nivel global de la clase de esta manera:
class Demarcaciones extends CActiveRecord
{
/**
* The followings are the available columns in table 'demarcaciones':
* @var integer $id
* @var string $referencia
* @var string $denominacion
* @var string $canal_multiple
* @var string $potencia_maxima
* @var string $superficie_total
* @var string $densidad_poblacion
* @var string $observaciones
*/
var $cantidad=0;
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'demarcaciones';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('referencia','length','max'=>10),
array('denominacion','length','max'=>64),
array('canal_multiple','length','max'=>3),
array('potencia_maxima','length','max'=>6),
array('superficie_total','length','max'=>45),
array('densidad_poblacion','length','max'=>45),
array('referencia, denominacion, canal_multiple, potencia_maxima, superficie_total, densidad_poblacion', 'required'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'municipios' => array(self::MANY_MANY, 'Municipios', 'rel_demarcaciones_municipios(id_demarcacion, id_municipio)','alias'=>'municipios'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'referencia' => 'Referencia',
'denominacion' => 'Denominacion',
'canal_multiple' => 'Canal Multiple',
'potencia_maxima' => 'Potencia Maxima',
'superficie_total' => 'Superficie Total',
'densidad_poblacion' => 'Densidad Poblacion',
'observaciones' => 'Observaciones',
);
}
}
Lo primero que me has comentado me devuelve la consulta de siempre como si no hiciese caso al criterio puesto:
Querying SQL: SELECT COUNT(DISTINCT `demarcaciones`.`id`) FROM `demarcaciones` LEFT OUTER JOIN `rel_demarcaciones_municipios` municipios_municipios ON (`demarcaciones`.`id`=municipios_municipios.`id_demarcacion`) LEFT OUTER JOIN `municipios` municipios ON (municipios.`id`=municipios_municipios.`id_municipio`)
Y la segunda opción me devuelve esto:
CException
Descripción
Propiedad "Demarcaciones"."queryScalar" no se encuentra definida.
Archivo Fuente:
C:\Servers\htdocs\yii\framework\db\ar\CActiveRecord.php(434)
00422: */
00423: public function __get($name)
00424: {
00425: if(isset($this->_attributes[$name]))
00426: return $this->_attributes[$name];
00427: else if(isset($this->getMetaData()->columns[$name]))
00428: return null;
00429: else if(isset($this->_related[$name]))
00430: return $this->_related[$name];
00431: else if(isset($this->getMetaData()->relations[$name]))
00432: return $this->getRelated($name);
00433: else
00434: return parent::__get($name);
00435: }
00436:
00437: /**
00438: * PHP setter magic method.
00439: * This method is overridden so that AR attributes can be accessed like properties.
00440: * @param string property name
00441: * @param mixed property value
00442: */
00443: public function __set($name,$value)
00444: {
00445: if($this->setAttribute($name,$value)===false)
00446: {
Stack Trace
#0 C:\Servers\htdocs\yii\framework\db\ar\CActiveRecord.php(434): CComponent->__get('queryScalar')
#1 C:\Servers\htdocs\aurora\protected\controllers\InfoGestionTdtController.php(220): CActiveRecord->__get('queryScalar')
#2 C:\Servers\htdocs\yii\framework\web\actions\CInlineAction.php(32): InfoGestionTdtController->actionCargarDemarcaciones()
#3 C:\Servers\htdocs\yii\framework\web\CController.php(300): CInlineAction->run()
#4 C:\Servers\htdocs\yii\framework\web\filters\CFilterChain.php(129): CController->runAction(Object(CInlineAction))
#5 C:\Servers\htdocs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()
#6 C:\Servers\htdocs\yii\framework\web\CController.php(957): CFilter->filter(Object(CFilterChain))
#7 C:\Servers\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))
#8 C:\Servers\htdocs\yii\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))
#9 C:\Servers\htdocs\yii\framework\web\CController.php(283): CFilterChain->run()
#10 C:\Servers\htdocs\yii\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)
#11 C:\Servers\htdocs\yii\framework\web\CWebApplication.php(310): CController->run('cargarDemarcaci...')
#12 C:\Servers\htdocs\yii\framework\web\CWebApplication.php(120): CWebApplication->runController('infoGestionTdt/...')
#13 C:\Servers\htdocs\yii\framework\base\CApplication.php(135): CWebApplication->processRequest()
#14 C:\Servers\htdocs\aurora\index.php(13): CApplication->run()
#15 {main}
Pero también veo que llega a realizar a preparar la consulta:
Querying SQL: SELECT count(*) as cantidad, `demarcaciones`.`id` AS `t0_c0`, municipios.`id` AS `t1_c0`, municipios.`nombre` AS `t1_c1`, municipios.`id_provincia` AS `t1_c2` FROM `demarcaciones` LEFT OUTER JOIN `rel_demarcaciones_municipios` municipios_municipios ON (`demarcaciones`.`id`=municipios_municipios.`id_demarcacion`) LEFT OUTER JOIN `municipios` municipios ON (municipios.`id`=municipios_municipios.`id_municipio`)
De todas formas lo que no comprendo es porque me pone el COUNT DISTINCT por defecto al usar el count normal… Imagino que será cosa por usar una tabla MANY_MANY de relaciones entre dos tablas.
Saludos.