Hi,
CakePHP Version: 2.x
I have 2 models which both of them use same custom behavior. I have to use those models in a single controller. Invoking desired method through the models at same time throws SQLSTATE[42000] error but if I comment one of invoked method, everything works fine!
Chart Behavior:
App::uses('ModelBehavior', 'Model');
class ChartBehavior extends ModelBehavior {
public function chart(Model $model, $deep = 'year') {
$dataModel = $model->find('all', array('fields' => $model->alias . '.addTime'));
$data = array();
$output = array();
for ($i = 0; $i < count($dataModel); $i++) {
$item = $dataModel[$i][$model->alias];
$year = date('Y', $item['addTime']);
$month = date('M', $item['addTime']);
$day = date('D', $item['addTime']);
$data[$year][$month][$day][] = $item['addTime'];
}
if ($deep == 'year') {
foreach ($data as $year => $monthArr) {
$output[$year] = 0;
foreach ($monthArr as $month => $dayArr) {
foreach ($dayArr as $day) {
$output[$year] += count($day);
}
}
}
} elseif ($deep == 'month') {
foreach ($data as $year => $monthArr) {
foreach ($monthArr as $month => $dayArr) {
$output[$month] = 0;
foreach ($dayArr as $day) {
$output[$month] += count($day);
}
}
}
} elseif ($deep == 'day') {
foreach ($data as $year => $monthArr) {
foreach ($monthArr as $month => $dayArr) {
foreach ($dayArr as $day => $items) {
$output[$day] = 0;
$output[$day] += count($items);
}
}
}
}
return $output;
}
}
Company Model:
class Company extends ManagementAppModel {
public $actsAs = array('Management.Chart','Management.Count');
}
Lottery Model:
class Lottery extends ManagementAppModel {
public $actsAs = array('Management.Chart','Management.Count');
}
Controller:
class StatsController extends ManagementAppController {
public $uses = array('Management.Lottery', 'Management.Company');
public $components = array('Management.Chart');
public function index() {
$lotteryData = $this->Lottery->chart('year');
$lotterySplited = $this->Chart->split($lotteryData);
$companyData = $this->Company->chart('year');
$companySplited = $this->Chart->split($companyData);
$this->set('lotteryTotalItemsData', json_encode($lotterySplited['data']));
$this->set('lotteryTotalItemsLabels', json_encode($lotterySplited['labels']));
$this->set('companyTotalItemsData', json_encode($companySplited['data']));
$this->set('companyTotalItemsLabels', json_encode($companySplited['labels']));
}
}
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘chart’ at line 1