Skip to content

Instantly share code, notes, and snippets.

@kylejohnson
Created May 4, 2013 19:17
Show Gist options
  • Select an option

  • Save kylejohnson/5518438 to your computer and use it in GitHub Desktop.

Select an option

Save kylejohnson/5518438 to your computer and use it in GitHub Desktop.
Attempting to return the event count on a per-monitor basis (lines 8-10 of the first file), instead its doing a count() on all monitors. The last file shows the query that is being run - why isn't WHERE `Event`.`MonitorId` IN ('1', '2') being appended to the query such as is for the find on line 6?
<?php
class Event extends AppModel {
public $useTable = 'Events';
public $belongsTo = array(
'Monitor' => array(
'className' => 'Monitor',
'foreignKey' => 'MonitorId'
)
);
}
?>
kjohnson@zoneminder:~/ZoneMinder/web/app/Model$ cat Monitor.php
<?php
class Monitor extends AppModel {
public $useTable = 'Monitors';
public $hasMany = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'MonitorId'
),
'Zone' => array(
'className' => 'Zone',
'foreignKey' => 'MonitorId'
)
);
}
?>
<?php
class MonitorsController extends AppController {
public function index() {
$monitoroptions['fields'] = array('Name', 'Id', 'Function', 'Host');
$this->set('monitors', $this->Monitor->find('all', $monitoroptions));
$elhoptions['conditions'] = array('Event.StartTime > DATE_SUB(NOW(), INTERVAL 1 HOUR)');
$elhoptions['fields'] = array('Event.Id');
$this->set('elh', $this->Monitor->Event->find('count', $elhoptions));
}
}
?>
SELECT COUNT(*) AS `count` FROM `zm`.`Events` AS `Event` LEFT JOIN `zm`.`Monitors` AS `Monitor` ON (`Event`.`MonitorId` = `Monitor`.`id`) WHERE `Event`.`StartTime` > DATE_SUB(NOW(), INTERVAL 1 HOUR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment