Ok this issue is driving me nutts, I thought that _id was meant to be ObjectID while the first time it inserts it does it correctly when I try to update it using the _id it does not work.
here is my code
//Save Data
function savedata($data){
$collection = $this->db->retail_logs;
$this->data = $data;
if($this->data['_id'] == NULL || $this->data['_id'] == "")
{
$this->data['_id'] = new MongoId();
}
else
{
$this->data['_id'] = ObjectID($this->data['_id']);
}
try {
$collection->update(
array("_id"=>$this->data['_id']),
$this->data, // new lead document to insert
array("upsert" => true, "safe" => true)
);
print $this->data['_id'];
} catch (Exception $e) {
print "we are not able to update";
}
}
i have tried to do the followinf
if($this->data['_id'] == NULL || $this->data['_id'] == "")
{
$this->data['_id'] = new MongoId();
}
else
{
$this->data['_id'] = ObjectID($this->data['_id']);
}
but that seems not to help.
What Is happening is it inserts the first time correctly with ObjectID(idnumber)
then when it goes to update is removes the ObjectID() and inserts a new lead with the same idnumber as before
so it looks like "IDNUMBER"
Your original code is close, but if you want to make a string _id the correct ObjectID type, use:
$this->data['_id'] = new MongoId($this->data['_id']);
Checking the Outcome of an Update Request
A non-upsert update may or may not modify an existing object. An upsert will either modify an existing object or insert a new object. The client may determine if its most recent message on a connection updated an existing object by subsequently issuing a getlasterror command ( db.runCommand( "getlasterror" ) ). If the result of the getlasterror command contains an updatedExisting field, the last message on the connection was an update request. If the updatedExisting field's value is true, that update request caused an existing object to be updated; if updatedExisting is false, no existing object was updated. An "upserted" field will contain the new _id value if an insert is performed (new as of 1.5.4)
Can you run the command as suggested by Mongo Docs and let us know the result of the command
Reference: Mongo Updating
Related
I've written a small php script to retrieve all the users in a certain group and obtain two values, username and employeeid. Unfortunately, the second field is always empty. But a similar query done in Go returns the value. I've read Adldap docs several times, but cannot figure out what's wrong.
This is the code I'm using:
$ad = new \Adldap\Adldap();
$ad->addProvider($config);
$userlist = [];
try {
$provider = $ad->connect();
$group = $provider->search()->groups()->find($groupname);
foreach ($group->getMembers() as $user) {
$userlist[] = [
'AccountName' => $user->getAccountName(),
'EmployeeId' => $user->getEmployeeId(),
];
}
} catch (\Adldap\Auth\BindException $e) {
echo $e->getMessage();
}
And this is the relevant working part in Go. Here I was retrieving only a single user element:
func BindAndSearch(l *ldap.Conn, username string) (*ldap.SearchResult, error) {
l.Bind(BindUsername, BindPassword)
searchReq := ldap.NewSearchRequest(
BaseDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
fmt.Sprintf(Filter, ldap.EscapeFilter(username)),
[]string{"employeeID"},
nil,
)
result, err := l.Search(searchReq)
...
Found this SO answer which is exactly my issue:
I was Connecting to the AD via port 3268. It seems some attributes can be fetched only by connecting to the AD via port 389.
I think I've worked out that the issue relates to binding params for PDO instead of including the values in the select statement.
Now googling how to bind array values where there will be a variable number of them per query type. For example there may be one, two or three category (col) values sought.
My primary script submits an ajax request to a secondary script.
Two params are sent and are being received in most scenarios.
eg department:mens-fashion, category:coats-and-jackets
In all but a few scenarios, the ajax query is successful and returns as expected. However, for some pairings of params, nothing is displayed in the page or in console.
Having returned from the query script immediately after building the query, I know the query is always working OK. (Copying and pasting into phpMyAmdin brings me the correct resultset.)
So it seems the key part of the script is the fetch routine. How can I catch an error message from PDO in the next line?...
$filtered_results = $filtered_statement->fetchAll(PDO::FETCH_ASSOC);
$debugInfo = array('debug' => vsprintf(str_replace("?", "%s", $filtered_statement->queryString), $opts ));
[edit]
if ( is_array($filtered_results) ) {
$filtered_results = array_merge($debugInfo, $filtered_results);
} else {
$filtered_results = $debugInfo;
}
[/edit]
debug info still only being returned if the query was a success.
$filtered_results_json = json_encode($filtered_results);
echo( $filtered_results_json );
Please would anyone point me to a solution where, the failed query will display in the calling script. The data required is in the db so I am still really stuck on trying to display failure messages.
You need to check whether an array is returned or not. If nothing was found no array is being returned - and you can't use array_merge.
$filtered_results = $filtered_statement->fetchAll(PDO::FETCH_ASSOC);
$debugInfo = array('debug' => vsprintf(str_replace("?", "%s", $filtered_statement->queryString), $opts ));
if ( is_array($filtered_results) ) {
$filtered_results = array_merge($debugInfo, $filtered_results);
} else {
$filtered_results = $debugInfo;
}
$filtered_results_json = json_encode($filtered_results);
echo( $filtered_results_json );
See this as well: Value return when no rows in PDO
I update an existing document with the code below. Its working fine.
foreach($jArray as $value){
!!some code!!
try {
$collection->update(array("tablename"=>$tablename),array('$push' => array("inventar" => $new_data)));
echo json_encode($collection);
}
catch ( MongoConnectionException $e ) {
echo '<p>Update failed</p>';
exit();
}
}
JSON response:
{"w":1,"wtimeout":10000}{"w":1,"wtimeout":10000}
(2 values are tried to update)
Even if no tablename matched, means no update happend, the result is w = 1.
Why? No update happend and w is 1/true?
There seems to be a little confusion here. The JSON response you are looking at is not the actual return value from the update operation. What you did was JSONifying the collection itself, which has the integer attributes w and wtimeout (see source code here). Those attributes are in no way related to the result of the update operation itself.
So, the right way to go seems to be changing the lines inside the scope of your try statement to:
$result = $collection->update(array("tablename"=>$tablename),array('$push' => array("inventar" => $new_data)));
echo json_encode($result);
For more information about what is returned from the update method, refer to these docs.
I have the following code which results sometimes in a silent failure:
public function updateCoreGameTableIfNecessary($coreEm)
{
$game = $this->getCoreGameRecord($coreEm);
if (!$game) {
$game = new Game();
$game->setHomeSchool($this->getHomeSchool()->getCoreSchool($coreEm));
$game->setDatetime($this->getDatetime()->format('Y-m-d H:i:s'));
$game->setDate($this->getDatetime()->getTimestamp());
$game->setTime($this->getDatetime()->format('H:i:s'));
$game->setSport($coreEm->getRepository('VNNCoreBundle:Sport')->findOneByName($this->getSport()->getName()));
$game->setSeason($coreEm->getRepository('VNNCoreBundle:Season')->findCurrent());
$game->setEventType(strtolower($this->getEventType()->getName()));
$game->setMeetName($this->getMeetName());
$game->setRemoteUnique(md5(rand(0, 100000)));
$game->setNotes($this->getRecap());
$game->setHomeConfId(0); // This field is no longer used, so value doesn't matter.
$game->setAwayConfId(0); // This field is no longer used, so value doesn't matter.
$game->setConfStatus(''); // This field is going away as well.
}
if ($this->getEventType()->getName() == 'Game') {
$game->setHomeScore($this->getHomeScore());
$game->setAwayScore($this->getAwayScore());
$game->setAwaySchool($this->getAwaySchool()->getCoreSchool($coreEm));
} else {
$game->setPlace($this->getPlace());
$game->setPoints($this->getHomeScore());
}
$game->setOwnerId($this->getUser()->getSchool()->getCoreSchool($coreEm)->getId());
$coreEm->persist($game);
$coreEm->flush();
return $game->getId();
}
It always starts with a $this that's already saved. For certain instances of $this (i.e. certain records in the database), $game won't get saved. I won't get an error or anything like that. It will just fail silently.
Any suggestions for debugging? I guess I'll try to figure out what's different about those certain records, but it seems like an insert should never silently fail, for any reason.
What is the best way to check if table exists in DynamoDb?
I would appreciate it if the code would be in PHP.
Either active or not.
* Added later as an example to various cases for error code 400
It's very easy to check if the table exist, it can have one of the following
TableStatus => CREATING, ACTIVE, DELETING or UPDATING
but in case i get error 400 it can mean more than one thing.
1) sent null string as a table name by mistake.
[x-aws-body] => {"TableName":""}
)
[body] => CFSimpleXML Object
(
[__type] => com.amazon.coral.validate#ValidationException
[message] => The paramater 'tableName' must be at least 3 characters long and at most 255 characters long
)
[status] => 400
2) syntax error in the command sent to DynamoDB, for example writting tabel_name instead of table_name.
[x-aws-body] => {"TabelName":"test7"}
)
[body] => CFSimpleXML Object
(
[__type] => com.amazon.coral.validate#ValidationException
[message] => The paramater 'tableName' is required but was not present in the request
)
[status] => 400
3) I would guess but didn't check, if I exceed at that same time the provisioned capacity on the table.
You can have a look at "describe_table" of the official PHP SDK. 400 means "does not exist" There is a pretty extensive example in the official documentation. Look at how it is used in the "delete" example, right at the bottom.
http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html
Here is the (stripped) example from the doc
<?php
require_once dirname(__FILE__) . '/sdk/sdk.class.php';
$dynamodb = new AmazonDynamoDB();
$table_name = 'ExampleTable';
$response = $dynamodb->describe_table(array('TableName' => $table_name));
if((integer) $response->status !== 400)
{
$error_type = $response->body->__type;
$error_code = explode('#', $error_type)[1];
if($error_code == 'ResourceNotFoundException')
{
echo "Table ".$table_name." exists.";
}
}
?>
Some of these answers are using the older SDK's and so I thought I'd update this useful question with what I coded up and works well. The newer exceptions really do make this task easier. This function gives you a nice boolean to use in scripts.
use Aws\DynamoDb\Exception\ResourceNotFoundException; // <-- make sure this line is at the top
public function TableExists($tableName) {
$ddb = DynamoDbClient::factory(array('region' => 'us-east-1')); // EC2 role security
try {
$result = $ddb->describeTable(array(
"TableName" => $tableName
));
} catch (ResourceNotFoundException $e) {
// if this exception is thrown, the table doesn't exist
return false;
}
// no exception thrown? table exists!
return true;
}
Hopefully this complete working code helps some of you.
I think the answer that solves this with describeTable is a good one, but fooling around with the status code response makes the code less readable and more confusing.
I chose to check for a tables existence using listTables. Here are the docs
$tableName = 'my_table';
$client = DynamoDbClient::factory(array('region' => 'us-west-2'));
$response = $client->listTables();
if (!in_array($tableName, $response['TableNames'])) {
// handle non-existence.
// throw an error if you want or whatever
}
// handle existence
echo "Table " . $tableName . " exists";
With DynamoDB you need to parse the contents of the error message in order to know what type of error you received since the status code is almost always 400. Here is a sample function that could work to determine if a table exists. It also allows you to specify a status as well if you want to check if it exists and if it is in a certain state.
<?php
function doesTableExist(AmazonDynamoDB $ddb, $tableName, $desiredStatus = null)
{
$response = $ddb->describe_table(array('TableName' => $tableName));
if ($response->isOK()) {
if ($desiredStatus) {
$status = $response->body->Table->TableStatus->to_string();
return ($status === $desiredStatus);
} else {
return true;
}
} elseif ($response->status === 400) {
$error = explode('#', $response->body->__type->to_string());
$error = end($error);
if ($error === 'ResourceNotFoundException') {
return false;
}
}
throw new DynamoDB_Exception('Error performing the DescribeTable operation.');
}
Update: In the AWS SDK for PHP 2, specific exceptions are thrown by the DynamoDB client, making this way easier to handle. Also, there are "Waiter" objects, including one for this use case (see usage in the unit test) that is designed to sleep until the table exists.
The above answers are correct if you just want to know whether the table exist or not. I want to make another helpful point here in case.
One should be very careful in multi-threaded or production level code.
Assuming that one thread deleted the table, then you will still get the answer that the table exists in answer to your query from second thread, until the table is fully deleted. In such case, once the table is deleted, the table handle in second thread is zombie, like the dangling pointer error in C++.
With dynamodb cli you can do it very simple as follows:
aws dynamodb describe-table --table-name "my-table"
If the table exists it returns
0 -- Command was successful. There were no errors thrown by either the CLI or by the service the request was made to.
If the table does not exist it returns
255 -- Command failed. There were errors thrown by either the CLI or by the service the request was made to.
See also:
http://docs.aws.amazon.com/cli/latest/reference/dynamodb/describe-table.html
http://docs.aws.amazon.com/cli/latest/topic/return-codes.html