I am having trouble updating an existing entity. Outside of my entity class
I have a class that creates a new instance of an entity, but first checks
for existence of the entity in the db by calling find($pri_key).
When find() is successful in finding the the entity, I then update the
entity and call flush(). Any code that uses this entity afterwards has the
correct values, however, the database is never updated!
I then found that if I call flush() outside the OAuthManager method that
does the actual setting of new values, the database is updated.
Basically this is not something abnormal I am trying to achieve, a simple
get row by id, if it exists, update it otherwise, create a new row.
class OAuthManager {
// ............... other code
private function _getAccessToken($auth_token) {
return $this->store()->entityManager()->find('OAuthAccessToken',
$auth_token);
}
public function getAccessToken($auth_token) {
$_OAuthAccessToken = $this->_getAccessToken($auth_token);
if(! $_OAuthAccessToken)
return NULL;
return array(
'client_id' => $_OAuthAccessToken->getClientId(),
'user_id' => $_OAuthAccessToken->getuserId(),
'expires' => $_OAuthAccessToken->getExpires()->getTimestamp(),
//DateTime object
'scope' => $_OAuthAccessToken->getScope(),
);
}
public function setAccessToken($oauth_token, $client_id, $user_id,
$expires, $scope = NULL) {
$_OAuthAccessToken = $this->_getAccessToken($oauth_token);
if(!$_OAuthAccessToken) {
API::log()->addWarning("setting new access token - " . $client_id);
$_OAuthAccessToken = new OAuthAccessToken();
$_OAuthAccessToken->setAccessToken($oauth_token);
$_OAuthAccessToken->setClientId($client_id);
$_OAuthAccessToken->setUserId($user_id);
$_OAuthAccessToken->setScope($scope);
$_OAuthAccessToken->setExpires($expires);
$this->store()->entityManager()->persist($_OAuthAccessToken);
} else {
API::log()->addWarning("updating existing access token - " .
$client_id);
$_OAuthAccessToken->setAccessToken($oauth_token);
$_OAuthAccessToken->setClientId($client_id);
$_OAuthAccessToken->setUserId($user_id);
$_OAuthAccessToken->setScope($scope);
$_OAuthAccessToken->setExpires($expires);
//
var_dump($this->store()->entityManager()->getUnitOfWork()->getScheduledEntityUpdates());
}
$this->store()->entityManager()->flush();
}
}
//inside the calling class i do this as a test
$om = new OAuthManager($doctrinestore);
$expires = new DateTime();
// this creates the new row in the database
$om->setAccessToken("tokenstring","some_client_id",1234,$expires);
// dumps out the array correctly as expected
var_dump($om->getAccessToken("tokenstring"));
// now update the data
$om->setAccessToken("tokenstring","new_client_id_value",6789,$expires);
// dumps out the array correctly as expected
var_dump($om->getAccessToken("tokenstring"));
When running the calling code above, the database still only shows the data
that was inserted initially in the first call to setAccessToken(), updated
values don't end up in the db, even though I can access the data via the
Entity after the update.
I tried adding:
$this->store()->entityManager()->getUnitOfWork()->getScheduledEntityUpdates()
to the OAuthManager::setAccessToken() method before the flush, but it is
always empty.
However, if I decide to remove the flush from the setAccessToken() method,
and call flush() in my client code. It the database row is updated as I
expect. This seems weird and would make development difficult. Can anyone
explain this behaviour, or have I missed something obvious?
My metadata for this Entity is as follows:
OAuthAccessToken:
type: entity
table: oauth_access_tokens
id:
accessToken:
type: string
nullable: false
length: 40
fixed: false
comment: ''
id: true
column: access_token
generator:
strategy: NONE
fields:
clientId:
type: string
nullable: false
length: 80
fixed: false
comment: ''
column: client_id
userId:
type: string
nullable: true
length: 255
fixed: false
comment: ''
column: user_id
expires:
type: datetime
nullable: false
comment: ''
default: CURRENT_TIMESTAMP
scope:
type: string
nullable: true
length: 2000
fixed: false
comment: ''
lifecycleCallbacks: { }
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.