ConditionExpression attribute_not_exists() - php

I am trying to use ConditionExpression when inserting an item on the database, but it dont work, the php script breaks when the Putitem() function runs.
I want to insert the item if he dont exist.
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
),
'ConditionExpression' => 'attribute_not_exists(serialNumber)'
));
I tried to var_dump the $response but the code breaks on the function above.
serialNumber its a Partition Key which should work as intended.
The code below works fine, but he replaces the existing item with new values, which its what i dont want to happen.
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
)
));

It is expected that you are returned a CondidtionCheckFailedException when the condition you set evaluates to false. Try wrapping your code in a try/catch block to see if it works as expected?
try {
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
)
));
}
catch(Exception $e) {
echo $e->getMessage();
}

Related

how to loop and array to create a TABLE in laravel with phpdocx

I am using phpdocx to generate an array with my data in a docx format.
$contact is an array of multiple object. Sometimes $contact contain 1 object, and sometimes more.
I want to make a loop, to add contact as much as I need.
My problem : For exemple, If I am doing this I will get an error like "Undefined array key 3" if my contact data only contain 3 object or less.
important : Here, if my datas contain 4 object (from 0 to 3 ) it will work but doesn't work when i have 2 objects.
$contact= array(
array(
'name' => $request->get('contact')[0]['name'],
'userName' => $request->get('contact')[0]['userName'],
'number' => $request->get('contact')[0]['number'],
'mail' => $request->get('contact')[0]['mail'],
),
array(
'name' => $request->get('contact')[1]['name'],
'userName' => $request->get('contact')[1]['userName'],
'number' => $request->get('contact')[1]['number'],
'mail' => $request->get('contact')[1]['mail'],
),
array(
'name' => $request->get('contact')[2]['name'],
'userName' => $request->get('contact')[2]['userName'],
'number' => $request->get('contact')[2]['number'],
'mail' => $request->get('contact')[2]['mail'],
),
array(
'name' => $request->get('contact')[3]['name'],
'userName' => $request->get('contact')[3]['userName'],
'number' => $request->get('contact')[3]['number'],
'mail' => $request->get('contact')[3]['mail'],
),
);
$docx->replaceTableVariable($contact, array('parseLineBreaks' => true));
what i am actually trying with no success for the moment : https://www.phpdocx.com/en/forum/default/topic/1773
I have make a loop and it is now working
$contactData = $request->get('contact');
$contactTab = array();
for ($i = 0; $i < count($contactData); $i++) {
$rowData = array(
'name' => $contactData[$i]['name'],
'userName' => $contactData[$i]['userName'],
'number' => $contactData[$i]['number'],
'mail' => $contactData[$i]['mail'],
);
$contactTab[] = $rowData;
}
$docx->replaceTableVariable($contactTab, array('parseLineBreaks' => true));

CakePHP multiple request

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

Amazon AWS, Creating a name tag for ec2 instance using PHP SDK 2

I'm using the latest version (2) of SDK for php. Below is a snippet of code for assigning a name tag to existing instance:
try {
$result = $ec2->createTags(array(
'Resources' => array($instanceid),
'Tags' => array(
'Name' => 'PWC_cwc'),
));
} catch (Exception $e) {
die("Failed to create tag name: ".$e."<br>");
}
Output:
Failed to create tag name: exception 'Guzzle\Service\Exception\ValidationException' with message 'Validation errors: [Tags][Name][Tag] must be of type object' in /Users/harry/Documents/workspace/BigData/vendor/guzzle/guzzle/src/Guzzle/Service/Command/AbstractCommand.php:394 Stack trace: #0
I'm guessing something is wrong with the way I pass the argument, but just couldn't figure out the correct way of doing this
The API link for createTags method is here: http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Ec2.Ec2Client.html#_createTags
You have to specify the 'Key' and 'Value' of each tag.
$args = array(
'DryRun' => False,
'Resources' => array($resource),
'Tags' => array(
array(
'Key' => 'firstkey',
'Value' => $firstkeyvalue),
array(
'Key' => 'secondkey',
'Value' => $secondkeyvalue),
array(
'Key' => 'thirdkey',
'Value' => $thirdkeyvalue)
));
$response = $ec2->createTags($args);
Try this:
$result = $ec2->createTags(array(
'Resources' => array($instanceid),
'Tags' => array(
'Tag' => array(
'Key' => '<key>',
'Value' => '<value>'
)
)
));
You need to have an array of 'Tag' inside the 'Tags' array.

if is_numeric condition issue

I have a http_build_query array and file_get_contents that looks like this:
$optionValues = http_build_query( array(
'leadcomm' => getVariable('LEADCOMM'),
'CCID' => getVariable('CCID'),
'QTR' => getVariable('QTR'),
'CLK' => getVariable('CLK'),
'dck' => getVariable('DCK'),
'bal_one' => getVariable('BAL_ONE'),
'product' => getVariable('PRODUCT'),
'prop_st' => getVariable('PROP_ST'),
'cred_grade' => getVariable('CRED_GRADE'),
'prop_zip' => getVariable('PROP_ZIP'),
'prop_desc' => getVariable('PROP_DESC'),
'spec_home' => getVariable('SPEC_HOME'),
'purchase_contract' => getVariable('PURCHASE_CONTRACT'),
'est_val' => getVariable('EST_VAL'),
'down_pmt' => getVariable('DOWN_PMT'),
'loan_type' => getVariable('LOAN_TYPE'),
'buy_timeframe' => getVariable('BUY_TIMEFRAME'),
'agent_found' => getVariable('AGENT_FOUND'),
'va_status' => getVariable('VA_STATUS'),
'income' => getVariable('INCOME'),
'annual_verifiable_income' => getVariable('ANNUAL_VERIFIABLE_INCOME'),
'fha_bank_foreclosure' => getVariable('FHA_BANK_FORECLOSURE'),
'num_mortgage_lates' => getVariable('NUM_MORTGAGE_LATES'),
'email' => getVariable('EMAIL'),
'fname' => getVariable('FNAME'),
'lname' => getVariable('LNAME'),
'address' => getVariable('ADDRESS'),
'city' => getVariable('CITY'),
'state' => getVariable('STATE'),
'zip' => getVariable('ZIP'),
'pri_phon' => getVariable('PRI_PHON'),
'sec_phon' => getVariable('SEC_PHON'),
'capture_method' => getVariable('CAPTURE_METHOD'),
'aid' => getVariable('AID'),
'srlp' => getVariable('SRLP'),
'scid' => getVariable('SCID'),
'buyer_id' => getVariable('BUYER_ID'),
'cid' => getVariable('CID'),
'ppcid' => getVariable('PPCID')
)
);
$url = 'http://api.example.com/import-lead-data.php';
$options['http'] = array(
'method' => "POST",
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $optionValues,
);
$context = stream_context_create($options);
$result = file_get_contents($url, NULL, $context);
I need to check if $results returns a numeric value. If it does, I need to set a new variable ($lead_id) to equal $results, pass $lead_id through another http_build_query array and finally do another file_get_contents $results. The code above works fine. The second part of the code is where I need some assistance. This is what I have for the second part of my code:
if (is_numberic($result)) {
$lead_id(isset($results));
};
$leadValues = http_build_query( array(
'lead_id' => "LEAD_ID"
)
);
$leadurl = 'http://api.bankradar.com/import-lead-response.php';
$leadoptions['http'] = array(
'method' => "POST",
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $leadValues,
);
$leadcontext = stream_context_create($leadoptions);
$leadResult = file_get_contents($leadurl, NULL, $leadcontext);
I think my problem is with the isset part of the code but I'm not sure.
you put
if (is_numberic($result)) {
should be
if (is_numeric($result)) {
But, i use this way
if (ctype_digit($result)) {
that way it can only containt digits, no decimals or any other characters... not sure if thats what your after
You are calling $lead_id as a function. Just change
$lead_id(isset($results));
to
$lead_id = $results;
You misspelled is_numeric (there's a b in yours) - is_numberic

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.

Categories