Duplicate rows in the database when refreshing the page laravel - database

there is a page which is updated every 5-7 seconds, and on it records from the base are updated, but moments these records are duplicated, Do not tell me why this bug can be?
is a ActiveCaller model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class ActiveCaller extends Model
{
protected $fillable = ['queue', 'employee_id', 'station', 'state', 'duration',
'client_id', 'last_call'];
public function Employee()
{
return $this->belongsTo(Employee::class);
}
/**
* Convert duration attribute to acceptable format
*
* #param $value
* #return string
*/
public function getDurationAttribute($value)
{
if (empty($value))
return $value;
return $this->sec2hms($value);
}
public function getStateAttribute($value)
{
if (!empty($value))
return trim($value);
return null;
}
/**
* Convert last call attribute to acceptable format
*
* #param $value
* #return string
*/
public function getLastCallAttribute($value)
{
$data = explode("\n", $value);
$result = "";
$i = 0;
$len = count($data) - 1;
foreach ($data as $item) {
$item = str_replace("\r", "", $item);
$delimiter = "</br>";
if ($i == $len)
$delimiter = "";
if (empty($item) || (trim($item) == "No calls yet")) {
$result .= "No calls yet$delimiter";
} else {
$result .= $this->sec2hms($item) . " min. ago $delimiter";
}
$i++;
}
return $result;
}
public function getStationAttribute($value)
{
return str_replace("\r\n", "</br>", $value);
}
private function sec2hms($sec, $padHours = FALSE)
{
$timeStart = Carbon::now();
$timeEnd = Carbon::now()->addSeconds(intval($sec));
return $timeStart->diff($timeEnd)->format('%H:%I:%S');
}
}
is a AmiApiController
class AmiApiController extends Controller
{
public function fetchDashboardData()
{
$this->updateQueueState();
$activeCallers = ActiveCaller::with('Employee')
->where('old', true)
->orderBy('queue')
->orderBy('employee_id')
->orderBy('station', 'asc')
->get();
$waitingList = WaitingList::where('old', false)->get();
$waitingList = $waitingList->unique('client_id');
$charts = Chart::all()->toArray();
$chartFormatData = [
'Total' => [],
'Callers' => [],
'Queues' => [],
];
foreach ($charts as $key => $chart) {
$charts[$key]['data'] = json_decode($chart['data'], 1);
$chartFormatData[$chart['name']]['total'] = 0;
foreach ($charts[$key]['data']['statistic'] as $datum) {
// if ($datum[0] === 'Effort')
// continue;
$chartFormatData[$chart['name']]['label'][] = $datum[0];
$chartFormatData[$chart['name']]['data'][] = $datum[1];
$chartFormatData[$chart['name']]['name'] = $chart['name'];
}
$chartFormatData[$chart['name']]['total'] = array_sum($chartFormatData[$chart['name']]['data']);
// $chartFormatData[$chart['name']]['label'] = array_reverse($chartFormatData[$chart['name']]['label']);
}
return response()->json([
'activeCallers' => $activeCallers,
'charts' => $chartFormatData,
'waitingList' => $waitingList
], 200);
}
this is where we begin to check if we can update the database at this time
/**
* Check whether the database can be updated at this time
*
* - Returns True if no updates are currently being made to the database
* and the latest update was less than 5 seconds later
* -
Returns True if the update already occurs for more than 15 seconds
*
* - Returns False if an update is already in the database
* -
Returns False if the last update was within the last 5 seconds
*
* If the parameter in $ json is passed true (by default)
* the method returns the answer in JSON format
*
* If the parameter is passed false to $ json
* method returns a php-shne Boolean value
*
* #param bool $json
* #return bool|\Illuminate\Http\JsonResponse
*/
public function canWeUpdate($json = true)
{
$result = ['return' => null, 'msg' => null];
$isUpdating = Configuration::where('key', 'is_ami_data_updating')->first();
if (is_null($isUpdating)) {
Configuration::create(['key' => 'is_ami_data_updating', 'value' => 0]);
}
if ($isUpdating->value == true) {
// if an update is currently in progress
$checkingDate = Carbon::now()->addSeconds(-10);
if ($isUpdating->updated_at < $checkingDate) {
// if the update is longer than 15 seconds, we will cancel this update
$isUpdating->update(['value' => false]);
$result['return'] = true;
$result['msg'] = "Old update in database";
} else {
// if the update is less than 15 seconds, we cannot update again
$result['return'] = false;
$result['msg'] = "CURRENTLY UPDATE";
}
} else if ($isUpdating->updated_at > Carbon::now()->addSeconds(-3)) {
// if the last update was less than 5 seconds ago, we cannot update
$result['return'] = false;
$result['msg'] = "TOO EARLY";
} else {
//if the last update was more than 5 seconds ago, we allow the update
$result['return'] = true;
$result['msg'] = "OK";
}
if ($json)
return $this->simpleResponse($result['return'], $result['msg']);
return $result['return'];
}
is a method fot check if new data is in the database
/**
* A method to check if new data is in the database
*
* Returns True if validation time is less than database update time
* Returns False if validation time is longer than database update time
* Returns False if there is no data in the database
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\JsonResponse
*/
public function checkForNewData(Request $request)
{
$date = new Carbon($request->last_call);
$lastRecord = ActiveCaller::latest()->first();
if (is_null($lastRecord))
return $this->simpleResponse(false);
if ($date < $lastRecord->updated_at) {
return $this->simpleResponse(true);
} else
return $this->simpleResponse(false);
}
/**
* Method for loading table data
*
* Agents - information about active numbers in the PBX in all queues
* Waiting - information about numbers in standby mode
*
* #return \Illuminate\Http\JsonResponse
*/
public function renderAgentTable()
{
$agents = ActiveCaller::with('Employee')
->where('old', true)
->orderBy('queue')
->orderBy('station', 'asc')
->get();
$waitingList = WaitingList::all();
$agentsTable = View::make('dashboard.render.agent-table', ['agents' => $agents]);
$waitingTable = View::make('dashboard.render.waiting-list-table', ['waitingList' => $waitingList]);
$result =
[
'agents' => $agentsTable->render(),
'waiting' => $waitingTable->render(),
];
$result = array_merge($result, $this->renderDashboardChart(new Request()));
return response()->json($result);
}
/**
* Method for updating data from AMI
*
* updating data for ActiveCaller
* updating data for WaitingList
*/
public function updateQueueState()
{
if (!$this->canWeUpdate(false)) {
// dd("We can't update (large check)");
return;
}
$lastUpdateTime = ActiveCaller::latest()->first();
if ($lastUpdateTime != null)
if ($lastUpdateTime->created_at > Carbon::now()->addSeconds(-3)) {
// dd("We can't update (small check)");
return;
}
// we notice the launch of the update in the database
$isAmiDataUpdating = Configuration::where('key', 'is_ami_data_updating')->first();
$isAmiDataUpdating->update(['value' => true]);
$this->ClearOldRecords();
$queues = Queue::where('on_dashboard', '=', '1')->get()->toArray();
// we go through all the queues that are available in AMI
foreach ($queues as $queue) {
$command = new AMIQueueMemberState($queue['name']);
// we get a list of numbers and a waiting list of calls
$response = $command->handle();
$agents = $response['agents'];
$callers = $response['callers'];
//convert the waiting list to PeerList
$peerList = PeerInfo::hydrate($agents);
$employees = new Collection();
foreach ($callers as $caller) {
$caller['queue'] = $queue['name'];
WaitingList::create($caller);
}
$peerList->each(function (PeerInfo $peer) use ($employees) {
$record = Record
::where('phone', 'like', '%' . $peer->name . '%')
->where('end_date', null)
->get()->first();
$data = null;
if ($record != null) {
// check if this user already has an entry in active_callers (not old)
$active = ActiveCaller
::where('employee_id', $record['employee_id'])
->where('old', false)
->get()->first();
// if so, add him another number and
// we move on to the next iteration
if ($active != null) {
if ($this->HandleSingleActive($active, $peer->name, $peer->last_call))
return;
}
$peer->station = $record['station_name'];
$peer->employee_id = $record['employee_id'];
$a = collect($peer->toArray());
$data = $a->except(['name', 'pause'])->toArray();
$data['station'] = "$peer->station | $peer->name";
} else {
$peer->station = "- | $peer->name";
$a = collect($peer->toArray());
$data = $a->except(['name', 'pause'])->toArray();
}
ActiveCaller::create($data);
});
}
$this->updateDashboardChart();
$isAmiDataUpdating->update(['value' => false]);
}

Related

Bypass spool when using thirdparty database spooling

I'm using a thirdparty bundle (Dextervip Citrax\DatabaseSwiftmailerBundle) to spool my email in a database. However, I still want to be able to bypass the spooling for some specific actions. Before, when I didn't use the bundle and used just regular spooling, I did this the following way:
public function sendSeparateMessage($subject, $fromEmail, $toEmail, $toCc, $body, $bypassSpool = false){
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setCc($toCc)
->setBody($body)
->setContentType("text/html");
// Send the message
$failedRecipients = [];
$numSent = 0;
foreach ($toEmail as $address => $name)
{
if (is_int($address)) {
$toEmail = $message->setTo($name);
} else {
$toEmail = $message->setTo([$address => $name]);
}
$numSent += $this->mailer->send($message, $failedRecipients);
}
if($bypassSpool) {
$spool = $this->mailer->getTransport()->getSpool();
$transport = \Swift_MailTransport::newInstance();
$spool->flushQueue($transport);
}
}
So whenever the last parameter of that function was set to true, the emails got sent out immediately and weren't stored for spooling.
For my situation now, I'd like to have the same thing, but of course I'm calling the wrong flushQueue function because I'm probably not accessing the right spooler. Does anybody know how to access the spooler from that bundle?
The service class from that bundle:
<?php
/**
* Created by PhpStorm.
* User: Rafael
* Date: 02/05/2015
* Time: 22:16
*/
namespace Citrax\Bundle\DatabaseSwiftMailerBundle\Spool;
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\Email;
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\EmailRepository;
use Swift_Mime_Message;
use Swift_Transport;
class DatabaseSpool extends \Swift_ConfigurableSpool {
/**
* #var EmailRepository
*/
private $repository;
private $parameters;
public function __construct(EmailRepository $repository, $parameters)
{
$this->repository = $repository;
$this->parameters = $parameters;
}
/**
* Starts this Spool mechanism.
*/
public function start()
{
// TODO: Implement start() method.
}
/**
* Stops this Spool mechanism.
*/
public function stop()
{
// TODO: Implement stop() method.
}
/**
* Tests if this Spool mechanism has started.
*
* #return bool
*/
public function isStarted()
{
return true;
}
/**
* Queues a message.
*
* #param Swift_Mime_Message $message The message to store
*
* #return bool Whether the operation has succeeded
*/
public function queueMessage(Swift_Mime_Message $message)
{
$email = new Email();
$email->setFromEmail(implode('; ', array_keys($message->getFrom())) );
if($message->getTo() !== null ){
$email->setToEmail(implode('; ', array_keys($message->getTo())) );
}
if($message->getCc() !== null ){
$email->setCcEmail(implode('; ', array_keys($message->getCc())) );
}
if($message->getBcc() !== null ){
$email->setBccEmail(implode('; ', array_keys($message->getBcc())) );
}
if($message->getReplyTo() !== null ){
$email->setReplyToEmail(implode('; ', array_keys($message->getReplyTo())) );
}
$email->setBody($message->getBody());
$email->setSubject($message->getSubject());
$email->setMessage($message);
$this->repository->addEmail($email);
}
/**
* Sends messages using the given transport instance.
*
* #param Swift_Transport $transport A transport instance
* #param string[] $failedRecipients An array of failures by-reference
*
* #return int The number of sent emails
*/
public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
{
if (!$transport->isStarted())
{
$transport->start();
}
$count = 0;
$emails = $this->repository->getEmailQueue($this->getMessageLimit());
foreach($emails as $email){
/*#var $message \Swift_Mime_Message */
$message = $email->getMessage();
try{
$count_= $transport->send($message, $failedRecipients);
if($count_ > 0){
$this->repository->markCompleteSending($email);
$count += $count_;
}else{
throw new \Swift_SwiftException('The email was not sent.');
}
}catch(\Swift_SwiftException $ex){
$this->repository->markFailedSending($email, $ex);
}
}
return $count;
}
}
and the services.yml file:
services:
repository.email:
class: Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\EmailRepository
factory: ['#doctrine.orm.default_entity_manager',getRepository]
arguments: ['CitraxDatabaseSwiftMailerBundle:Email']
citrax.database.swift_mailer.spool:
class: Citrax\Bundle\DatabaseSwiftMailerBundle\Spool\DatabaseSpool
arguments: ['#repository.email', '%citrax_database_swift_mailer.params%']
swiftmailer.spool.db:
alias: citrax.database.swift_mailer.spool
swiftmailer.mailer.default.spool.db:
alias: citrax.database.swift_mailer.spool

check field weather expired or not while loading a page cakephp

The following function checks if the banner is old enough to make it inactive by setting the status field in the Banner table to 0.
/**
*
* Checks weather the banners are old enough to be turned back to non-premium
* and change the status to 0 , in case of non-premium
*
* #return void
*/
protected function check_status(){
$non_premium_expire = 5*24*60*60;
$premium_expire = 14*24*60*60;
$data = $this->Banner->find('all');
foreach ($data as $Banners) {
if ($Banners['Banner']['is_premium'] == 0) {
if ((time() - strtotime($Banners['Banner']['created'])) > $premium_expire) {
$this->Banner->id = $Banners['Banner']['id'];
$this->Banner->saveField('status',0);
}
else if ($Banners['Banner']['is_premium'] == 1) {
if ((time() - strtotime($Banners['Banner']['created'])) > $non_) {
$this->Banner->id = $Banners['Banner']['id'];
$this->Banner->saveField('status',0);
}
}
}
}
}
from mybanners() function. i.e. the check_status() function should be called when users open
/site-name/mybanners
I have called the check_status() function from mybanners() function shown below
public function mybanners()
{
$this->check_status();
$this->layout = 'index';
$this->loadModel('Banner');
$loggedUserId = $this->Auth->user('id');
My problem is that the one field status is not getting updated in the database even after trying set() and updateAll() function. Any solutions? I am newbie in cakephp
I figured it out. The working code is below.
**
*
* Checks weather the banners are old enough to be turned back to non-premium
* and change the status to 0 , in case of non-premium
*
* #return void
*/
protected function check_status(){
$non_premium_expire = 5*24*60*60;
$premium_expire = 14*24*60*60;
$data = $this->Banner->find('all');
//pr ($Banners);
//pr ((time() - strtotime($Banners['Banner']['modified'])) > 5*24*60*60);
//pr ((time() - strtotime($Banners['Banner']['modified'])) > 14*24*60*60);
//die();
foreach ($data as $Banners) {
if ($Banners['Banner']['is_premium'] == 0) {
if ((time() - strtotime($Banners['Banner']['modified'])) > $non_premium_expire) {
$id = $Banners['Banner']['id'];
//pr ("in non premium loop");
//pr ($id);
//die();
$data_one = array('id' => $id , 'status'=> 0, 'modified' => false);
$this->Banner->save($data_one);
//$this->Banner->clear();
}
}
if ($Banners['Banner']['is_premium'] == 1) {
if ((time() - strtotime($Banners['Banner']['modified'])) > $premium_expire) {
$id = $Banners['Banner']['id'];
//pr ("in premium loop");
//pr ($id);
//die();
$data_one = array('id' =>$id , 'status'=> 0, 'modified' => false);
//pr ($data_one);
//die();
$this->Banner->save($data_one);
//$this->Banner->clear();
}
}
}
}

cakephp get query logs when debug is set to 0

How can you get a list of the executed queries when debug is set to 0 ?
I have the following functions in AppController:
// log INSERT, UPDATE, DELETE queries
private function __logQueries(){
$db = &ConnectionManager::getDataSource('default');
$query_logs = $db->getLog();
if( isset($query_logs['log']) && !empty($query_logs['log']) ){
$this->loadModel('SqlLog');
foreach($query_logs['log'] as $log_q){
$query = $log_q['query'];
if( stristr($query, 'INSERT ') || stristr($query, 'UPDATE ') || stristr($query, 'DELETE ') ){
$q_data = array(
'query' => $query,
'page' => $this->Misc->getCurrentPageURL(), // get the current page
'referrer' => trim(env('HTTP_REFERER')) == '' ? '' : env('HTTP_REFERER'),
'ip' => env('REMOTE_ADDR'),
'browser' => env('HTTP_USER_AGENT'),
'user_id' => SessionComponent::check('Auth.User') ? SessionComponent::read('Auth.User.id') : 0,
'username' => SessionComponent::check('Auth.User') ? addslashes(SessionComponent::read('Auth.User.username')) : '',
);
$this->SqlLog->create();
$this->SqlLog->save($q_data);
}
}
} // end if query occured
}
public function beforeRedirect(){
$this->__logQueries();
}
public function afterFilter(){
$this->__logQueries();
}
The insert, update and delete queries get logged when debug is set to 2, but how can I log those queries when debug is set to 0?
Whether queries are being logged depends on the DboSource::$fullDebug property, it's being set in the DboSource constructur, and by default it's true in case debug is > 1.
So all you have to do is overwrite the value of this property with true, this will ensure that as of this point the queries are being logged regardless of the debug setting.
App::uses('ConnectionManager', 'Model');
ConnectionManager::getDataSource('default')->fullDebug = true;
Create your own dbo file in the specified path:
\app\models\datasources\dbo\dbo_mysql_with_log.php
and the content of this file will be
<?php
class DboMysqlBase extends DboSource {
/**
* Description property.
*
* #var string
*/
var $description = "MySQL DBO Base Driver";
/**
* Start quote
*
* #var string
*/
var $startQuote = "`";
/**
* End quote
*
* #var string
*/
var $endQuote = "`";
/**
* use alias for update and delete. Set to true if version >= 4.1
*
* #var boolean
* #access protected
*/
var $_useAlias = true;
/**
* Index of basic SQL commands
*
* #var array
* #access protected
*/
var $_commands = array(
'begin' => 'START TRANSACTION',
'commit' => 'COMMIT',
'rollback' => 'ROLLBACK'
);
/**
* MySQL column definition
*
* #var array
*/
var $columns = array(
'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
'string' => array('name' => 'varchar', 'limit' => '255'),
'text' => array('name' => 'text'),
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
'binary' => array('name' => 'blob'),
'boolean' => array('name' => 'tinyint', 'limit' => '1')
);
/**
* Generates and executes an SQL UPDATE statement for given model, fields, and values.
*
* #param Model $model
* #param array $fields
* #param array $values
* #param mixed $conditions
* #return array
*/
function update(&$model, $fields = array(), $values = null, $conditions = null) {
if (!$this->_useAlias) {
return parent::update($model, $fields, $values, $conditions);
}
if ($values == null) {
$combined = $fields;
} else {
$combined = array_combine($fields, $values);
}
$alias = $joins = false;
$fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions));
$fields = join(', ', $fields);
$table = $this->fullTableName($model);
if (!empty($conditions)) {
$alias = $this->name($model->alias);
if ($model->name == $model->alias) {
$joins = implode(' ', $this->_getJoins($model));
}
}
$conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
if ($conditions === false) {
return false;
}
if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) {
$model->onError();
return false;
}
return true;
}
/**
* Generates and executes an SQL DELETE statement for given id/conditions on given model.
*
* #param Model $model
* #param mixed $conditions
* #return boolean Success
*/
function delete(&$model, $conditions = null) {
if (!$this->_useAlias) {
return parent::delete($model, $conditions);
}
$alias = $this->name($model->alias);
$table = $this->fullTableName($model);
$joins = implode(' ', $this->_getJoins($model));
if (empty($conditions)) {
$alias = $joins = false;
}
$conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
if ($conditions === false) {
return false;
}
if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
$model->onError();
return false;
}
return true;
}
/**
* Sets the database encoding
*
* #param string $enc Database encoding
*/
function setEncoding($enc) {
return $this->_execute('SET NAMES ' . $enc) != false;
}
/**
* Returns an array of the indexes in given datasource name.
*
* #param string $model Name of model to inspect
* #return array Fields in table. Keys are column and unique
*/
function index($model) {
$index = array();
$table = $this->fullTableName($model);
if ($table) {
$indexes = $this->query('SHOW INDEX FROM ' . $table);
if (isset($indexes[0]['STATISTICS'])) {
$keys = Set::extract($indexes, '{n}.STATISTICS');
} else {
$keys = Set::extract($indexes, '{n}.0');
}
foreach ($keys as $i => $key) {
if (!isset($index[$key['Key_name']])) {
$col = array();
$index[$key['Key_name']]['column'] = $key['Column_name'];
$index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0);
} else {
if (!is_array($index[$key['Key_name']]['column'])) {
$col[] = $index[$key['Key_name']]['column'];
}
$col[] = $key['Column_name'];
$index[$key['Key_name']]['column'] = $col;
}
}
}
return $index;
}
/**
* Generate a MySQL Alter Table syntax for the given Schema comparison
*
* #param array $compare Result of a CakeSchema::compare()
* #return array Array of alter statements to make.
*/
function alterSchema($compare, $table = null) {
if (!is_array($compare)) {
return false;
}
$out = '';
$colList = array();
foreach ($compare as $curTable => $types) {
$indexes = array();
if (!$table || $table == $curTable) {
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
foreach ($types as $type => $column) {
if (isset($column['indexes'])) {
$indexes[$type] = $column['indexes'];
unset($column['indexes']);
}
switch ($type) {
case 'add':
foreach ($column as $field => $col) {
$col['name'] = $field;
$alter = 'ADD '.$this->buildColumn($col);
if (isset($col['after'])) {
$alter .= ' AFTER '. $this->name($col['after']);
}
$colList[] = $alter;
}
break;
case 'drop':
foreach ($column as $field => $col) {
$col['name'] = $field;
$colList[] = 'DROP '.$this->name($field);
}
break;
case 'change':
foreach ($column as $field => $col) {
if (!isset($col['name'])) {
$col['name'] = $field;
}
$colList[] = 'CHANGE '. $this->name($field).' '.$this->buildColumn($col);
}
break;
}
}
$colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
$out .= "\t" . join(",\n\t", $colList) . ";\n\n";
}
}
return $out;
}
/**
* Generate a MySQL "drop table" statement for the given Schema object
*
* #param object $schema An instance of a subclass of CakeSchema
* #param string $table Optional. If specified only the table name given will be generated.
* Otherwise, all tables defined in the schema are generated.
* #return string
*/
function dropSchema($schema, $table = null) {
if (!is_a($schema, 'CakeSchema')) {
trigger_error(__('Invalid schema object', true), E_USER_WARNING);
return null;
}
$out = '';
foreach ($schema->tables as $curTable => $columns) {
if (!$table || $table == $curTable) {
$out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
}
}
return $out;
}
/**
* Generate MySQL index alteration statements for a table.
*
* #param string $table Table to alter indexes for
* #param array $new Indexes to add and drop
* #return array Index alteration statements
*/
function _alterIndexes($table, $indexes) {
$alter = array();
if (isset($indexes['drop'])) {
foreach($indexes['drop'] as $name => $value) {
$out = 'DROP ';
if ($name == 'PRIMARY') {
$out .= 'PRIMARY KEY';
} else {
$out .= 'KEY ' . $name;
}
$alter[] = $out;
}
}
if (isset($indexes['add'])) {
foreach ($indexes['add'] as $name => $value) {
$out = 'ADD ';
if ($name == 'PRIMARY') {
$out .= 'PRIMARY ';
$name = null;
} else {
if (!empty($value['unique'])) {
$out .= 'UNIQUE ';
}
}
if (is_array($value['column'])) {
$out .= 'KEY '. $name .' (' . join(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
} else {
$out .= 'KEY '. $name .' (' . $this->name($value['column']) . ')';
}
$alter[] = $out;
}
}
return $alter;
}
/**
* Inserts multiple values into a table
*
* #param string $table
* #param string $fields
* #param array $values
*/
function insertMulti($table, $fields, $values) {
$table = $this->fullTableName($table);
if (is_array($fields)) {
$fields = join(', ', array_map(array(&$this, 'name'), $fields));
}
$values = implode(', ', $values);
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
}
}
class DboMysql extends DboMysqlBase {
/**
* Enter description here...
*
* #var unknown_type
*/
var $description = "MySQL DBO Driver";
/**
* Base configuration settings for MySQL driver
*
* #var array
*/
var $_baseConfig = array(
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cake',
'port' => '3306',
'connect' => 'mysql_pconnect'
);
/**
* Connects to the database using options in the given configuration array.
*
* #return boolean True if the database could be connected, else false
*/
function connect() {
$config = $this->config;
$connect = $config['connect'];
$this->connected = false;
if (!$config['persistent']) {
$this->connection = mysql_connect($config['host'] . ':' . $config['port'], $config['login'], $config['password'], true);
} else {
$this->connection = $connect($config['host'] . ':' . $config['port'], $config['login'], $config['password']);
}
if (mysql_select_db($config['database'], $this->connection)) {
$this->connected = true;
}
if (isset($config['encoding']) && !empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
$this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">=");
return $this->connected;
}
/**
* Disconnects from database.
*
* #return boolean True if the database could be disconnected, else false
*/
function disconnect() {
if (isset($this->results) && is_resource($this->results)) {
mysql_free_result($this->results);
}
$this->connected = !#mysql_close($this->connection);
return !$this->connected;
}
/**
* Executes given SQL statement.
*
* #param string $sql SQL statement
* #return resource Result resource identifier
* #access protected
*/
function _execute($sql) {
return mysql_query($sql, $this->connection);
}
/**
* Returns an array of sources (tables) in the database.
*
* #return array Array of tablenames in the database
*/
function listSources() {
$cache = parent::listSources();
if ($cache != null) {
return $cache;
}
$result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
if (!$result) {
return array();
} else {
$tables = array();
while ($line = mysql_fetch_array($result)) {
$tables[] = $line[0];
}
parent::listSources($tables);
return $tables;
}
}
/**
* Returns an array of the fields in given table name.
*
* #param string $tableName Name of database table to inspect
* #return array Fields in table. Keys are name and type
*/
function describe(&$model) {
$cache = parent::describe($model);
if ($cache != null) {
return $cache;
}
$fields = false;
$cols = $this->query('DESCRIBE ' . $this->fullTableName($model));
foreach ($cols as $column) {
$colKey = array_keys($column);
if (isset($column[$colKey[0]]) && !isset($column[0])) {
$column[0] = $column[$colKey[0]];
}
if (isset($column[0])) {
$fields[$column[0]['Field']] = array(
'type' => $this->column($column[0]['Type']),
'null' => ($column[0]['Null'] == 'YES' ? true : false),
'default' => $column[0]['Default'],
'length' => $this->length($column[0]['Type']),
);
if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) {
$fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']];
}
}
}
$this->__cacheDescription($this->fullTableName($model, false), $fields);
return $fields;
}
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
* #param string $data String to be prepared for use in an SQL statement
* #param string $column The column into which this data will be inserted
* #param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
* #return string Quoted and escaped data
*/
function value($data, $column = null, $safe = false) {
$parent = parent::value($data, $column, $safe);
if ($parent != null) {
return $parent;
}
if ($data === null || (is_array($data) && empty($data))) {
return 'NULL';
}
if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
return "''";
}
if (empty($column)) {
$column = $this->introspectType($data);
}
switch ($column) {
case 'boolean':
return $this->boolean((bool)$data);
break;
case 'integer':
case 'float':
if ($data === '') {
return 'NULL';
}
if ((is_int($data) || is_float($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
$data[0] != '0' && strpos($data, 'e') === false)) {
return $data;
}
default:
$data = "'" . mysql_real_escape_string($data, $this->connection) . "'";
break;
}
return $data;
}
/**
* Returns a formatted error message from previous database operation.
*
* #return string Error message with error number
*/
function lastError() {
if (mysql_errno($this->connection)) {
return mysql_errno($this->connection).': '.mysql_error($this->connection);
}
return null;
}
/**
* Returns number of affected rows in previous database operation. If no previous operation exists,
* this returns false.
*
* #return integer Number of affected rows
*/
function lastAffected() {
if ($this->_result) {
return mysql_affected_rows($this->connection);
}
return null;
}
/**
* Returns number of rows in previous resultset. If no previous resultset exists,
* this returns false.
*
* #return integer Number of rows in resultset
*/
function lastNumRows() {
if ($this->hasResult()) {
return mysql_num_rows($this->_result);
}
return null;
}
/**
* Returns the ID generated from the previous INSERT operation.
*
* #param unknown_type $source
* #return in
*/
function lastInsertId($source = null) {
$id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
return $id[0]['insertID'];
}
return null;
}
/**
* Converts database-layer column types to basic types
*
* #param string $real Real database-layer column type (i.e. "varchar(255)")
* #return string Abstract column type (i.e. "string")
*/
function column($real) {
if (is_array($real)) {
$col = $real['name'];
if (isset($real['limit'])) {
$col .= '('.$real['limit'].')';
}
return $col;
}
$col = str_replace(')', '', $real);
$limit = $this->length($real);
if (strpos($col, '(') !== false) {
list($col, $vals) = explode('(', $col);
}
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
return $col;
}
if (($col == 'tinyint' && $limit == 1) || $col == 'boolean') {
return 'boolean';
}
if (strpos($col, 'int') !== false) {
return 'integer';
}
if (strpos($col, 'char') !== false || $col == 'tinytext') {
return 'string';
}
if (strpos($col, 'text') !== false) {
return 'text';
}
if (strpos($col, 'blob') !== false || $col == 'binary') {
return 'binary';
}
if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {
return 'float';
}
if (strpos($col, 'enum') !== false) {
return "enum($vals)";
}
return 'text';
}
/**
* Enter description here...
*
* #param unknown_type $results
*/
function resultSet(&$results) {
if (isset($this->results) && is_resource($this->results) && $this->results != $results) {
mysql_free_result($this->results);
}
$this->results =& $results;
$this->map = array();
$numFields = mysql_num_fields($results);
$index = 0;
$j = 0;
while ($j < $numFields) {
$column = mysql_fetch_field($results,$j);
if (!empty($column->table)) {
$this->map[$index++] = array($column->table, $column->name);
} else {
$this->map[$index++] = array(0, $column->name);
}
$j++;
}
}
/**
* Fetches the next row from the current result set
*
* #return unknown
*/
function fetchResult() {
if ($row = mysql_fetch_row($this->results)) {
$resultRow = array();
$i = 0;
foreach ($row as $index => $field) {
list($table, $column) = $this->map[$index];
$resultRow[$table][$column] = $row[$index];
$i++;
}
return $resultRow;
} else {
return false;
}
}
/**
* Gets the database encoding
*
* #return string The database encoding
*/
function getEncoding() {
return mysql_client_encoding($this->connection);
}
}
class DboMysqlWithLog extends DboMysql {
function _execute($sql) {
//exclude these types of queries from being logged, change if required
if(!preg_match('/^SHOW|SELECT|DESCRIBE|cake_sessions.*/',$sql) && !preg_match('/^SET.*/',$sql)){
//HH_DOCUMENT_ROOT should be defined in one of the core files pointing to the project root directory, if its not there define it
//do not commit changes, if its specific to local machine
//$filename = HH_DOCUMENT_ROOT.DS.'sql.txt';
$filename = 'C:\xampp\htdocs\your_project_dir\app\sql.txt';
$patfile = fopen($filename, 'a+');
$textdata = "$sql\n";
fwrite($patfile, $textdata);
fclose($patfile);
}
return parent::_execute($sql);
}
}
?>
change sql log file path $filename = 'C:\xampp\htdocs\your_project_dir\app\sql.txt';
Step 2: Open /app/config/database.php
and change 'driver' => 'dbo_mysql_with_log',
This will record all your sql scripts in the below path
C:\xampp\htdocs\your_project_dir\app\sql.txt

How to use cakephp export txt file

I'm using cakephp framework.I want to export a database table of a client download in two formats.One is '.csv', the other is a '.txt'.CSV which I have passed 'csvHelper' resolved.Now I want to know how to export '.txt'.Sorry for my poor English, but I think you'll see what I mean, thanks!
Basically you set the header for the filename, like this:
$this->response->type('Content-Type: text/csv');
$this->response->download('myfilename.txt');
Here are some functions I put in my appController, so that any controller can output a CSV file if required:
/**
* Flattens the data that is returned from a find() operation, and puts it into CSV format
* #param $data
* #return string
*/
public function _arrayToCsvFile($data){
$flattenedData = array();
$exportKeyPair = array();
foreach($data as $datumKey => $datumDetails){
$flattenedDatum = Hash::flatten($datumDetails, '.');
$flattenedData[] = $flattenedDatum;
// Find all keys
if($datumKey == 0){
$exportKeys = array_keys($flattenedDatum);
$exportKeyPair = array_combine($exportKeys, $exportKeys);
}
else {
$datumKeys = array_keys($flattenedDatum);
$datumKeyPair = array_combine($datumKeys, $datumKeys);
// Add the datum keys to the exportKeyPair if it's not already there
$exportKeyPair = array_merge($exportKeyPair, $datumKeyPair);
}
}
$exportKeyMap = array();
foreach($exportKeyPair as $exportKey => $exportValue){
$exportKeyMap[$exportKey] = "";
}
$outputCSV = '';
$outputCSV .= $this->_arrayToCsvLine($exportKeyPair);
foreach($flattenedData as $flattenedDatumKey => $flattenedDatumDetails){
// Add any extra keys
$normalisedDatumDetails = array_merge($exportKeyMap, $flattenedDatumDetails);
$outputCSV .= $this->_arrayToCsvLine($normalisedDatumDetails);
}
return $outputCSV;
}
/**
* arrayToCsvLine function - turns an array into a line of CSV data.
*
* #access public
* #param mixed $inputLineArray the input array
* #param string $separator (default: ")
* #param string $quote (default: '"')
* #return string
*/
function _arrayToCsvLine($inputLineArray, $separator = ",", $quote = '"') {
$outputLine = "";
$numOutput = 0;
foreach($inputLineArray as $inputLineKey => $inputLineValue) {
if($numOutput > 0) {
$outputLine .= $separator;
}
$outputLine .= $quote . str_replace(array('"', "\n", "\r"),array('""', "", ""), $inputLineValue) . $quote;
$numOutput++;
}
$outputLine .= "\n";
return $outputLine;
}
/**
* Serves some CSV contents out to the client
* #param $csvContents
* #param array $options
*/
public function _serveCsv($csvContents, $options = array()){
$defaults = array(
'modelClass' => $this->modelClass,
'fileName' => null
);
$settings = array_merge($defaults, $options);
if(empty($settings['fileName'])){
$settings['fileName'] = strtolower( Inflector::pluralize( $settings['modelClass'] ) ) . '_' . date('Y-m-d_H-i') . '.csv';
}
$this->autoRender = false;
$this->response->type('Content-Type: text/csv');
$this->response->download($settings['fileName']);
$this->response->body($csvContents);
}
Now in any controller you can do this (note that you can pass the filename to _serveCsv if required):
/**
* admin_export_csv method
*
* #return void
*/
public function admin_export_csv() {
$this->Order->recursive = 0;
$conditions = array();
$orders = $this->Order->find('all', array('conditions' => $conditions));
$csvContents = $this->_arrayToCsvFile($orders); // Function in AppController
$this->_serveCsv($csvContents, array('filename' => 'export.txt')); // Function in AppController
}
you can use
$this->RequestHandler->respondAs('txt'); to output the correct header.
Please not the header is not set when DEBUG is greater than 2.
And you will still have to build the controller action and set view as ususal.

Problem in executing Cron Jobs

i have done a bidding site in Cake PHP.The main problem I am facing is I need to run CRON JOBS on the server.But I dont' know why it is bugging me.I have craeted a controller called 'deamons' and there 4 different actions of it,which I want to run continuously on the server every minute,so that we can run the Autobidder set by each user of that bidding site.
The Cron Jobs I am setting up are...
curl -s -o /dev/null http://www.domain.com/app/webroot/daemons/bidbutler
curl -s -o /dev/null http://www.domain.com/app/webroot/daemons/extend
curl -s -o /dev/null http://www.domain.com/app/webroot/daemons/autobid
curl -s -o /dev/null http://www.domain.com/app/webroot/daemons/close
and the the controller which is handling all the stuff is attached below....!!!
Please suggest me some solution to this so that
If the experts wants to test it..the URL is www.domain.com/app/webroot
And here is the code...which I am trying to run through the CRONS...!!!
<?php
class DaemonsController extends AppController {
var $name = 'Daemons';
var $uses = array('Auction', 'Setting');
function beforeFilter(){
$email='nishant.nightcrawler#gmail.com';
$secondemail='no-reply#bidoppo.com';
$mess='It works';
//#mail($email, 'Test', $mess, "From: ".$secondemail);
parent::beforeFilter();
if(!empty($this->Auth)) {
$this->Auth->allow('bidbutler', 'extend', 'autobid', 'close');
}
ini_set('max_execution_time', ($this->appConfigurations['cronTime'] * 60) + 1);
}
/**
* The function makes the bid butler magic happen
*
* #return array Affected Auction
*/
function bidbutler() {
$this->layout = 'js/ajax';
$data = array();
$setting = array();
$auctions = array();
// Get the bid butler time
$bidButlerTime = $this->Setting->get('bid_butler_time');
// Get various settings needed
$data['bid_debit'] = $this->Setting->get('bid_debit');
$data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
$data['auction_time_increment'] = $this->Setting->get('auction_time_increment');
$data['auction_peak_start'] = $this->Setting->get('auction_peak_start');
$data['auction_peak_end'] = $this->Setting->get('auction_peak_end');
$expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
while (time() < $expireTime) {
// Formating the conditions
$conditions = array(
'Auction.end_time < \''. date('Y-m-d H:i:s', time() + $bidButlerTime). '\'',
'Auction.closed' => 0,
'Bidbutler.bids >' => 0
);
// Find the bidbutler entry - we get them from the lowest price to the maximum price so that they all run!
$this->Auction->Bidbutler->contain('Auction');
$bidbutlers = $this->Auction->Bidbutler->find('all', array('conditions' => $conditions, 'order' => 'rand()', 'fields' => array('Auction.id', 'Auction.start_price', 'Bidbutler.id', 'Bidbutler.minimum_price', 'Bidbutler.maximum_price', 'Bidbutler.user_id'), 'contain' => 'Auction'));
if(!empty($bidbutlers)) {
// Walk through bidbutler entries
foreach($bidbutlers as $bidbutler) {
if($bidbutler['Bidbutler']['minimum_price'] >= $bidbutler['Auction']['start_price'] &&
$bidbutler['Bidbutler']['maximum_price'] < $bidbutler['Auction']['start_price']) {
// Add more information
$data['auction_id'] = $bidbutler['Auction']['id'];
$data['user_id'] = $bidbutler['Bidbutler']['user_id'];
$data['bid_butler'] = $bidbutler['Bidbutler']['id'];
// Bid the auction
$result = $this->Auction->bid($data);
}
}
}
usleep(900000);
}
}
/**
* The function auto extends auctions and bids for an auto bid if neccessary
*
* #return array Affected Auction
*/
function extend() {
$this->layout = 'js/ajax';
$data = array();
$setting = array();
$auctions = array();
$data['bid_debit'] = $this->Setting->get('bid_debit');
$data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
$data['auction_time_increment'] = $this->Setting->get('auction_time_increment');
$data['auction_peak_start'] = $this->Setting->get('auction_peak_start');
$data['auction_peak_end'] = $this->Setting->get('auction_peak_end');
$data['isPeakNow'] = $this->isPeakNow();
$expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
while (time() < $expireTime) {
// now check for auto extends
$auctions = Cache::read('daemons_extend_auctions');
if(empty($auctions)) {
$auctions = $this->Auction->find('all', array('contain' => '', 'conditions' => "(Auction.extend_enabled = 1 OR Auction.autobid = 1) AND (Auction.start_price < Auction.minimum_price) AND Auction.winner_id = 0 AND Auction.closed = 0"));
Cache::write('daemons_extend_auctions', $auctions, '+1 day');
}
if(!empty($auctions)) {
foreach($auctions as $auction) {
// lets see if we need to extend the auction
$endTime = strtotime($auction['Auction']['end_time']);
$extendTime = time() + ($auction['Auction']['time_before_extend']);
if($extendTime > $endTime) {
// lets see if autobid is enabled
// autobid will place a bid by a robot if another user is the highest bidder but hasn't meet the minimum price
if($auction['Auction']['autobid'] == 1) {
if($auction['Auction']['extend_enabled'] == 1) {
// lets only bid if the limit is less than te autobid limit when the autobid limit is set
if($auction['Auction']['autobid_limit'] > 0) {
if($auction['Auction']['current_limit'] <= $auction['Auction']['autobid_limit']) {
$this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data);
}
} else {
$this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data);
}
} else {
$bid = $this->Auction->Bid->lastBid($auction['Auction']['id']);
// lets set the autobid
if(!empty($bid) && ($bid['autobidder'] == 0)) {
$this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data);
}
}
} elseif($auction['Auction']['extend_enabled'] == 1) {
unset($auction['Auction']['modified']);
$auction['Auction']['end_time'] = date('Y-m-d H:i:s', $endTime + ($auction['Auction']['time_extended']));
// lets do a quick check to make sure the new end time isn't less than the current time
$newEndTime = strtotime($auction['Auction']['end_time']);
if($newEndTime < time()) {
$auction['Auction']['end_time'] = date('Y-m-d H:i:s', time() + ($auction['Auction']['time_extended']));
}
$this->Auction->save($auction);
}
}
}
}
usleep(800000);
}
}
/**
* The function auto extends auctions in the last IF the extend function fails
*
* #return array Affected Auction
*/
function autobid() {
$data['bid_debit'] = $this->Setting->get('bid_debit');
$data['auction_time_increment'] = $this->Setting->get('auction_time_increment');
$data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
$data['auction_peak_start'] = $this->Setting->get('auction_peak_start');
$data['auction_peak_end'] = $this->Setting->get('auction_peak_end');
$data['isPeakNow'] = $this->isPeakNow();
$isPeakNow = $this->isPeakNow();
$expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
while (time() < $expireTime) {
// lets start by getting all the auctions that have closed
$auctions = $this->Auction->find('all', array('fields' => array('Auction.id', 'Auction.peak_only'), 'contain' => '', 'conditions' => "Auction.winner_id = 0 AND Auction.end_time <= '" . date('Y-m-d H:i:s', time() + 4) . "' AND Auction.closed = 0"));
if(!empty($auctions)) {
foreach($auctions as $auction) {
// before we declare this user the winner, lets run some test to make sure the auction can definitely close
if($this->Auction->checkCanClose($auction['Auction']['id'], $isPeakNow, false) == false) {
// lets check to see if the reason we can't close it, is because its now offpeak and this is a peak auction
if($auction['Auction']['peak_only'] == 1 && !$isPeakNow) {
continue;
} else {
$this->Auction->Autobid->placeAutobid($auction['Auction']['id'], $data);
}
}
}
}
usleep(900000);
}
}
/**
* The function closes the auctions
*
* #return array Affected Auction
*/
function close() {
$expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
while (time() < $expireTime) {
// lets start by getting all the auctions that have closed
$auctions = $this->Auction->find('all', array('contain' => '', 'conditions' => "Auction.winner_id = 0 AND Auction.end_time <= '" . date('Y-m-d H:i:s') . "' AND Auction.closed = 0"));
if(!empty($auctions)) {
foreach($auctions as $auction) {
$isPeakNow = $this->isPeakNow();
// before we declare this user the winner, lets run some test to make sure the auction can definitely close
if($this->Auction->checkCanClose($auction['Auction']['id'], $isPeakNow) == false) {
// lets check to see if the reason we can't close it, is because its now offpeak and this is a peak auction
if($auction['Auction']['peak_only'] == 1 && !$isPeakNow) {
$peak = $this->nonPeakDates();
//Calculate how many seconds auction will end after peak end
$seconds_after_peak = strtotime($auction['Auction']['end_time']) - strtotime($peak['peak_end']);
$end_time = strtotime($peak['peak_start']) + $seconds_after_peak;
$auction['Auction']['end_time'] = date('Y-m-d H:i:s', $end_time);
$this->Auction->save($auction);
} else {
// lets check just how far ago this auction closed, and either place an autobid or extend the time
$data['auction_time_increment'] = $this->Setting->get('auction_time_increment');
$newEndTime = strtotime($auction['Auction']['end_time']);
if($newEndTime < time() - $data['auction_time_increment']) {
$auction['Auction']['end_time'] = date('Y-m-d H:i:s', time() + ($auction['Auction']['time_extended']));
$this->Auction->save($auction);
} else {
//lets extend it by placing an autobid
$data['bid_debit'] = $this->Setting->get('bid_debit');
$data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
$data['auction_peak_start'] = $this->Setting->get('auction_peak_start');
$data['auction_peak_end'] = $this->Setting->get('auction_peak_end');
$data['isPeakNow'] = $this->isPeakNow();
$this->Auction->Autobid->placeAutobid($auction['Auction']['id'], $data);
}
}
continue;
}
$bid = $this->Auction->Bid->find('first', array('conditions' => array('Bid.auction_id' => $auction['Auction']['id']), 'order' => array('Bid.id' => 'desc')));
if(!empty($bid)) {
if($bid['User']['autobidder'] == 0) {
// send the email to the winner
$data['Auction'] = $auction['Auction'];
$data['Bid'] = $bid['Bid'];
$data['User'] = $bid['User'];
$data['to'] = $data['User']['email'];
$data['subject'] = sprintf(__('%s - You have won an auction', true), $this->appConfigurations['name']);
$data['template'] = 'auctions/won_auction';
$this->_sendEmail($data);
$auction['Auction']['status_id'] = 1;
}
$auction['Auction']['winner_id'] = $bid['Bid']['user_id'];
}
unset($auction['Auction']['modified']);
$auction['Auction']['closed'] = 1;
$this->Auction->save($auction);
}
}
usleep(900000);
}
}
}
?>
The CakePHP way to run crons is to build your own shells to do the tasks. Shells allows you full access to all of your controllers through the command prompt. Be sure to read this documentation when starting:
http://book.cakephp.org/view/108/The-CakePHP-Console
It shows you how to build your own shells (app/vendors/shells/), how to organize your shells into tasks, and how to properly run your shell as a cron job.
I do it a slightly different way than the documentation describes. My cron statement looks like:
* * * * * (cd /path/to/my/cake/app; sh ../cake/console/cake daily;) 1> /dev/null 2>&1
From there I simply have a shell called app/vendors/shells/daily.php
<?php
class DailyShell extends Shell {
var $uses = array('User');
function main() {
$this->User->processDailyTasks();
}
}
?>
This is far better and more stable than using curl in a cron job.

Resources