CakePHP multiple request - php

I am facing the issue of multiple request at same time.I am checking in DB if this data exists then not insert the same record.
Using cakephp 2.6 version
$userdata = $this->MobappFan->find('first', array(
'conditions' => array(
'MobappFan.id' => 123,
'MobappFan.mobapp_id' => 234,
'MobappFan.deleted' => 0
),
'fields' => array('MobappFan.id', 'MobappFan.status', 'MobappFan.blocked'),
'contain' => false
)
);
if(!$userdata) {
$this->MobappFan->save($this->request->data);
}
But due to same request with same parameters is not able to find the existing record.
Please help
Thanks

I think basically you just want to not check in the current posted id
you can do this as below
$userdata = $this->MobappFan->find('first', array(
'conditions' => array(
'MobappFan.id' => 123,
'MobappFan.id !=' => $this->request->data['MobappFan']['id'],
'MobappFan.mobapp_id' => 234,
'MobappFan.deleted' => 0
),
'fields' => array('MobappFan.id', 'MobappFan.status', 'MobappFan.blocked'),
'contain' => false
)
);
if(!$userdata) {
$this->MobappFan->save($this->request->data);
}
or
$userdata = $this->MobappFan->find('first', array(
'conditions' => array(
'MobappFan.id' => 123,
"NOT" => array( 'MobappFan.id' => $this->request->data['MobappFan']['id']),
'MobappFan.mobapp_id' => 234,
'MobappFan.deleted' => 0
),
'fields' => array('MobappFan.id', 'MobappFan.status', 'MobappFan.blocked'),
'contain' => false
)
);
if(!$userdata) {
$this->MobappFan->save($this->request->data);
}
Hope this helps. If you can explain more what you need then I can write a better conditions or you can check details here

Related

aws SDK 3, PHP, runInstance does not create the supplied Tags

$options = array(
'UserData' => base64_encode('test'),
'SecurityGroupIds' => [AWS_REGIONS[$region]['security_group']],
'InstanceType' => AWS_REGIONS[$region]['instance_type'],
'ImageId' => AWS_REGIONS[$region]['ami'],
'MaxCount' => $to_launch,
'MinCount' => 1,
//'EbsOptimized' => true,
'SubnetId' => AWS_REGIONS[$region]['subnet_id'],
'Tags' => [['Key' => 'task', 'Value' => $task],['Key' => 'Name', 'Value' => $task]],
'InstanceInitiatedShutdownBehavior' => 'terminate'
);
$response = $client->runInstances($options);
I am using the "latest" Ec2Client
It launches fine but the Tags are completely ignored.
I suspect an error within the EC2 API but I am not that experienced.
Maybe someone with experience can help me out ?
This is because Ec2Client::runInstances does not have tags option
http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-10-01.html#runinstances
You would need to make a separate call to tag newly created instance(s) using Ec2Client::createTags:
$result = $client->createTags(array(
'DryRun' => true || false,
// Resources is required
'Resources' => array('string', ... ),
// Tags is required
'Tags' => array(
array(
'Key' => 'string',
'Value' => 'string',
),
// ... repeated
),
));
Read more here:
http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-10-01.html#createtags

CakePHP find all not setting values to variables. All other find functions working

In my controller I have two functions that pull all records from the db with a status = 4. In one function it works fine. I copied the find all statement from the working function:
$this->set('completed', $this->Topic->find('all', array('fields' => array(
'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
'Topic.status' => 4))));
But in the new function the $completed array doesn't seem to exist. The debug statement is just a blank line. If I debug the sql log using debug($this->Topic->getDataSource()->getLog()); this is the returned array:
Array
(
[log] => Array
(
[0] => Array
(
[query] => SELECT `Topic`.`creator`, `Topic`.`link`, `Topic`.`id`, `Topic`.`topic_name`, `Topic`.`info`, `Topic`.`priority`, `Topic`.`user_id`, `Topic`.`completed`, `Topic`.`created`, `Topic`.`status`, `User`.`name`, `User`.`id`, `Topic`.`category`, `Topic`.`tags` FROM `topics` AS `Topic` LEFT JOIN `users` AS `User` ON (`Topic`.`user_id` = `User`.`id`) LEFT JOIN `events` AS `Event` ON (`Event`.`topic_id` = `Topic`.`id`) WHERE `Topic`.`status` = 4
[affected] => 9
[numRows] => 9
[took] => 0
)
)
[count] => 1
[time] => 0
)
The SQL statement in the log works. If I plug in into mySQL it produces results. And the affected and numRows field show the correct number of records. But the produced data isn't being set to variables. Any ideas would be greatly appreciated. My boss and I are stumped. Here are both complete functions:
public function dashboard(){
$this->set('completed', $this->Topic->find('all', array('fields' => array(
'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
'Topic.status' => 4))));
$this->set('total_inprogress_release', $this->Topic->find('count', array('conditions' => array(
'OR' => array('status <>' => 4,'status <>' => 0),
'priority' => 4))));
$this->set('upcoming_events', $this->Topic->Event->find('all'));
$this->Topic->virtualFields['count'] = 'COUNT(*)';
$this->set('graph_data', $this->Topic->find('chart', array('fields' => array('status_txt', 'priority_txt', 'count'), 'group' => array('status', 'priority'),
'chart' => array(
'xaxisTitle' => 'Status',
'yaxisLabels' => array('Release', 'Company', 'News'),
'xaxisLabels' => array('Open','In Progress','Completed'),
'yaxis' => 'Topic.priority_txt',
'xaxis' => 'Topic.status_txt',
'data' => 'Topic.count'
))));
}
And here's the nonworking function:
public function completed(){
$foo = $this->Topic->find('all', array('fields' => array(
'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
'Topic.status' => 4)));
debug($foo);
debug($this->Topic->getDataSource()->getLog());
}
After several hours of tracking the data until we figured out where it was being lost, we found that the problem was some of the data in the database. It was encoded and it was causing a while statement in the core cake files to reset when it encountered said data. The solution was to alter Config/database.php as such:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'login',
'password' => 'Password',
'database' => 'database',
'prefix' => '',
'encoding' => 'utf8',//this line allows the database to read utf8 data.
);
}
I think the relationships may be causing an issue here. Try this in the completed function and pr($foo) the results:
$foo = $this->Topic->find('all', array(
'fields' => array(
'Topic.creator',
'Topic.link',
'Topic.id',
'Topic.topic_name',
'Topic.info',
'Topic.priority',
'Topic.user_id',
'Topic.completed',
'Topic.created',
'Topic.status',
'User.name',
'User.id',
'Topic.category',
'Topic.tags'
),
'conditions' => array(
'Topic.status' => 4,
),
'joins' => array(
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'Topic.user_id = User.id'
),
),
),
'recursive' => -1,
));
I suspect you will get the results you are looking for.

CakePHP check two find conditions at the same time

I have the following find code:
if($this->User->find('first', array(
'conditions' => array(
'User.username' => $this->data['User']['username'],
'User.email' => $this->data['User']['username'])
)))
The idea is that it will find if a user exists with an email or username that matches what was sent in the username input field.
The problem is that in fact it will try and match BOTH rather than one or the other which is what I really want. How do I do this though?
I tried with the following but still no success:
if($this->User->find('first', array(
'conditions' => array('OR' => array(
array('User.username' => $this->data['User']['username']),
array('User.email' => $this->data['User']['username'])))))
Thanks
You were close, it should go like this
$opts = array(
'conditions' => array(
'or' => array(
'User.username' => $this->data['User']['username'],
'User.email' => $this->data['User']['username']
)
)
)
$this->User->find('first', $opts)

CakePHP paginator not paginating

I have a paginator as follows:
var $paginate = array(
'order'=>array('ReleaseServer.server_environment'=>'ASC',
'ReleaseServer.server_name'=>'ASC'),
'joins'=>array(
array(
'table' => 'release_server_to_components',
'alias' => 'ReleaseServerToComponent',
'type' => 'LEFT',
'foreignKey' => false,
'conditions'=> array('ReleaseServer.id = ReleaseServerToComponent.release_server_id')
),
array(
'table' => 'release_components',
'alias' => 'ReleaseComponent',
'type' => 'LEFT',
'foreignKey' => false,
'conditions'=> array('ReleaseServerToComponent.release_component_id = ReleaseComponent.id')
)
),
'group'=>array('ReleaseServer.id'),
'contain' => array(
'ReleaseServerToComponent' => array(
'ReleaseComponent' => array(
'Release'
)
)
),
'limit' => 25,
);
Then in my controller function I do the following:
$this->set('allServers', $this->paginate('ReleaseServer', $conditions));
Where $conditions are some extra conditions for the query.
As you see above I set the limit at 25.
There are 29 records in the database however, but the page only shows 25 and the page says there is only one page.
but when a person clicks on one of the column headers to order them, some rows that were not there before magically appear, and others disappear. Why would this be?
If you need any other info please let me know
UPDATE
Now i see that the problem resides in the group part of the paginate variable, but i need it in order to make it so I do not get multiple rows of the same thing.
How do I fix that?
I solved it by adding a new paginateCount() function to my model:
function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
$count = $this->find('count', array(
'fields' => 'DISTINCT ReleaseServer.id',
'conditions' => $conditions
));
return $count;
}

CakePHP, wrting a better query string

In Cakephp is there a better way to write this:
$unread_orders = $this->Order->find('all', array('conditions' => array('Order.status' => 'unread') ));
$read_orders = $this->Order->find('all', array('conditions' => array('Order.status' => 'read') ));
$dispatched = $this->Order->find('all', array('conditions' => array('Order.status' => 'dispatched'), 'limit' => 5));
$canceled = $this->Order->find('all', array('conditions' => array('Order.status' => 'canceled'), 'limit' => 5));
There is a limit on the dispatched and canceled items.
It's seems like there would be a more effcient way of doing this, in one call to the database instead of 4.
Cheers.
One way is to do
$orders_read_unread = $this->Order->find('all', array('conditions' => array('OR' => array(array('Order.status' => 'unread'), array('Order.status' => 'read')))));
$orders_disp_cancel = $this->Order->find('all', array('conditions' => array('OR' => array(array('Order.status' => 'canceled'), array('Order.status' => 'dispatched'))), 'limit' => 5));
EDIT: Updated queries. Thanks Mark for clarifying.
<?php
...
$orders_read_unread = $this->Order->find( 'all', array(
'conditions' => array( 'Order.status' => array( 'unread', 'read' )),
'group' => array( 'Order.status' ),
));
/**
* Use this if you need 5 of EITHER canceled OR dispatched order
* if you need 5 of EACH you need to break it into two queries.
*/
$orders_dispatched_canceled = $this->Order->find( 'all', array(
'conditions' => array( 'Order.status' => array( 'canceled', 'dispatched' )),
'group' => array( 'Order.status' ),
'limit' => 5
));
/**
* Use these if you need 5 of EACH dispatched AND canceled orders
*/
$orders_dispatched = $this->Order->find( 'all', array(
'conditions' => array( 'Order.status' => 'dispatched' ),
'group' => array( 'Order.status' ),
'limit' => 5
));
$orders_canceled = $this->Order->find( 'all', array(
'conditions' => array( 'Order.status' => 'canceled' ),
'group' => array( 'Order.status' ),
'limit' => 5
));
...
?>
Should do the trick for you without having to deal with the 'OR' key syntax. It will generate a slightly less efficient IN ARRAY( '..' ,'..' ) syntax but keeps the php a little cleaner.
As an alternative you could look at either sub-queries - which are a pain with Cake. The book has an example of using the query builder via the datasource to inject a query into the conditions array of a normal cake find call.
http://book.cakephp.org/view/1030/Complex-Find-Conditions
And remember both of these finds should be in the model inside a function - you can either define a custom find type or just call a model function directly from your controller.
http://www.pixelastic.com/blog/88:using-custom-find-and-paginate-methods-in-cakephp-1-3

Categories