Order repository in observer returns 'No such entity with orderId' magento 2 - magento2

When event 'controller_action_predispatch_checkout_index_index' is executed I am trying to get order from database (not the one that will be created after checkout, the one that already exists in database and which is created before.). I am loading order through order repository and it works.
use Magento\Sales\Api\OrderRepositoryInterface as OrderRepository;
$this->orderRepository->get($approvalOrderId);
When I try to load the same order and with the same code sample when event 'sales_order_place_after' is executed it is not working. I am getting error 'No such entity with orderId = ..'

It might be that the order is not yet saved at that moment. But the event you are observing will already carry that order object. Can you try the below code in the observers execute function?
public function execute(EventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
//do something with the $order
}

Related

SAPUI5 batch submit returns error

I am using the following code, in an attempt to batch upload the changes made on a table:
onConfirmActionPressed: function() {
var oModel = this.getModel();
oModel.setUseBatch(true);
oModel.submitChanges();
}
I am using setProperty() to set the new values, like this:
onSingleSwitchChange: function(oControlEvent) {
var oModel = this.getView().getModel();
var rowBindingContext = oControlEvent.getSource().getBindingContext();
oModel.setProperty(rowBindingContext.sPath + "/Zlspr", "A");
}
When onConfirmActionPressed is executed, I get a server error, saying that "Commit work during changeset processing not allowed" on SAP R3.
When I upload the lines of the table one-by-one, it works fine. However, uploading this way is very slow, and in some cases it takes more than 10 minutes for the process to complete.
Am I doing something wrong while batch submitting? Is there a chance the issue is due to server (R3) misconfiguration?
You need to override methods:
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END
Keep track of the errors across all calls to update methods and if everything went OK then in changeset_end perform commit on the database
edit:
To clarify:
In your Data Provider Class Extension in SAP Gateway you need to find your YOURENTITY_UPDATE_ENTITY method and get rid off any COMMIT WORK statements.
Then you need to redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN method and, which is a method which is fired before any batch operation. You could define a class attribute such as table mt_batch_errors which would be emptied in this method.
When you post batch changes from UI5 using oModel.submitChanges() all single changes to Entities are directed to appropriate ..._UPDATE_ENTITY methods. You need to keep track of any possible errors and if any occurs then fill your mt_batch_errors table.
After all entities have been updated /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END method is fired in which you are able to check mt_batch_errors table if any errors occurred during the batch process. If there were errors then you should probably ROLLBACK WORK, and if not then you are free to COMMIT WORK.
That is just an example of how it could be done, I'm curious of other suggestions.
Good luck!

Save update of one Doctrine document from preUpdate hook of another document?

I have an event subscriber for DocumentA. DocumentA has associated documents of type DocumentB. During the preUpdate lifecycle event hook for DocumentA, I'd like to refresh a value on its DocumentB. I have code like so:
public function preUpdate(LifecycleEventArgs $args)
{
$document = $args->getDocument();
if (!($document instanceof DocumentA) ||
return;
}
if ($documentsB = $document->getDocumentB()) {
$dm = $args->getDocumentManager();
foreach (iterator_to_array($documentsB) as $docB) {
$documentB = $dm->find(DocumentB::class, $docB->getId());
$documentB->setFooCode();
$dm->merge($documentB);
}
}
}
I've tried this with $dm->persist($documentB) instead of using merge(), I've set DocumentA's relationship to DocumentB to cascade: {all}, and I've tried $dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($class, $document); for both DocumentA and each DocumentB, but I don't seem to be getting anywhere. I don't seem to be able to call flush() even for a single DocumentB without causing a segfault (I'm assuming it triggers an infinite loop of preUpdate events inside preUpdate events?)
How do I save the changes to my associated documents when the changes are made in the preUpdate method of DocumentA's event subscriber?
I elaborated on this further in one of your previous questions, but to reiterate from Doctrine's documentation:
Changes to associations of the updated entity are never allowed in this event
-Changes to associations of the passed entities are not recognized by the flush operation anymore.
With the level of complexity you are trying to handle in a listener, I think you would be better off making a service that handles some of this and call that instead.

Zend Framework 1.12, execution of raw SQL inserts the same record twice

I know there´s a similar situation in stack, and I´ve already checked it out and the answers have not guided me to mine.
Here´s the deal:
I need to execute raw SQL, an INSERT to be precise. I have multiple values to insert as well. No problem since Zend let´s you use "query" from Zend_Db_Table (I use my default adapter, initialized in my application.ini file).
$db_adapter = Zend_Db_Table::getDefaultAdapter();
....query gets built...
$stmt = $db_adapter->query($query);
$stmt->execute();
When doing var_dump($query); this is what I see:
INSERT INTO tableName (col1,col2,col3) VALUES ('value1', 'value2', 'value3')
I have already tried the following:
I have no "manifesto" attribute on the html page rendered
I have looked at the network both with Chrome and Firefox to see the POSTS/GETS getting sent, and the controller whose actions does
this INSERT gets called once with a POST.
It is true that I call a viewscript from another action to be rendered, like so:
$this->renderScript('rough-draft/edit.phtml');
I don´t see how that could affect the statement getting executed twice.
I know it gets executed twice:
Before the POST is sent, the data base has nothing, clean as a whistle
POST gets sent, logic happens, from is rendered again
Data base now has the same record twice (with the name that has been inserted from the POST), so it´s not a rendering problem
It has to do with the way I am executing raw SQL, because if I use ->insert($data) method that comes with Zend_Db_Table_Abstract (obviously I declare a class that extends from this abstract one and bind it to the appropriate table) it does not insert the record twice. Thing is, I want to use raw SQL.
I really appreciate your help!
Expanded as requested:
Controller code:
public function saveAction()
{
$request = $this->getRequest();
if (!$request->isPost())
$this->_sendToErrorPage();
$this->_edit_roughdraft->saveForm($request->getPost());
$this->view->form = $this->_edit_roughdraft->getForm();
$this->renderScript('rough-draft/edit.phtml');
}
Code from model class (in charge of saving the form from the data in POST):
public function saveForm($form_data) {
$db = new Application_Model_DbTable_RoughDrafts();
$id = $form_data['id'];
$db->update($this->_get_main_data($form_data), array('id=?' => $id));
/* Main data pertains to getting an array with the data that belongs to yes/no buttons
* and text input. I need to do it as so because the other data belongs to a dynamically
* built (by the user) multi_select where the options need to be deleted or inserted, that
* happens below
*/
$multi_selects = array('responsables', 'tareas', 'idiomas', 'habilidades', 'perfiles');
foreach($multi_selects as $multi_select){
if(isset($form_data[$multi_select]) && !empty($form_data[$multi_select])){
$function_name = '_save_'.$multi_select;
$this->$function_name($form_data[$multi_select], $id);
}
}
$this->_form = $this->createNewForm($id, true);
//The createNewForm(..) simply loads data from data_base and populates form
}
I do want to make clear though that I have also used $db->insert($data), where $db is a class that extends from Zend_Db_Table_Abstract (associated to a table). In this case, the record is inserted once and only once. That´s what leads me to believe that the problem lies within the execution of my raw sql, I just don´t know what is wrong with it.
I should have done this from the beginning, thanks for the help everyone, I appreciate your time.
I knew the problem was with my syntax, but I didn´t know why. After looking at different examples for how people did the raw sql execution, I still didn´t understand why. So I went ahead and opened the class Zend_Db_Adapter_Abstract in Zend library and looked for the 'query()' method I was using to get the statement to execute.
public function query($sql, $bind = array())
{
...some logic...
// prepare and execute the statement with profiling
$stmt = $this->prepare($sql);
**$stmt->execute($bind);**
// return the results embedded in the prepared statement object
$stmt->setFetchMode($this->_fetchMode);
return $stmt;
}
So the ->query(..) method from the Zend_Db_Adapter already executes said query. So if I call
->execute() on the statement that it returns, I'll be the one causing the second insert.
Working code:
$db_adapter = Zend_Db_Table::getDefaultAdapter();
....query gets built...
$db_adapter->query($query);

Entity Framework 4.0 - The underlying provider failed on Open

We have a web application with Entity Framework 4.0. Unfortunately, when the large volume of users hit the application the EF throws an error
The underlying provider failed on Open
Below is the code snippet:
//DAL
public IQueryable<EmployeeEntity> GetEmployeeDetail()
{
DatabaseEntities ent = new DatabaseEntities(this._connectionString);
IQueryable<EmployeeEntity> result = from employee in ent.EmployeeEntity
select employee;
return result;
}
Please note the above code returns IQuerable.
Is anything wrong with above pattern that could cause the exception to occur?
When and how does Entity Framework determine to close / open db connection and also how long to retain?
On what scenario does above error occurs?
What's the maximum number of connection pool for EF and how do we configure?
Do we need to explicitely specify open and close
Is code below a best way to resolve above issue?
public IQueryable<EmployeeEntity> GetEmployeeDetail()
{
using (DatabaseEntities ent = new DatabaseEntities(this._connectionString))
{
IQueryable<EmployeeEntity> result = from employee in ent.EmployeeEntity
select employee;
return result.ToList().AsQuerable();
}
}
The ToList() call will cause the query to run on the database immediately and as this is not filtered in any way will return every employee in your database. This is probably likely to cause you performance issues.
However you can't remove this in your case because if you return the IQueryable directly then the context will be disposed by the time you try and fetch results.
You could either:
change the way it works so that the scope of ent does not end when the method returns and return the query without calling ToList(). You can then further filter the IQueryable before calling ToList().
call ToList() within the method but filter/limit the query first (e.g. pass some parameters into the method to specify this) to reduce the number of rows coming back from the database.

Update issue with Generic Repositories using EF

I have created Generic Repository and I have two entities that i need to update in a transaction.
Here is what i m doing..
ProductOrganizationEntity poDataContext= new ProductOrganizationEntity();
IRepository<tblProductInfo> productRepo = new GenericRepository<ProductOrganizationEntity, tblConfirmation>(poDataContext);
Piece of Code which is causing problem is this.
using (TransactionScope txScope = new TransactionScope())
{
productRepo.Attach(productEntity);
productRepo.SaveChanges();
new ProductLocation().SaveLocation(productEntity.Locations, productEntity.productCode);
txScope.Complete();
}
productRepo.SaveChanges(); This is where it throws me Error. The error is
The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "Venus" was unable to begin a distributed transaction.
(We do have server named Venus but its not access in anyway in these transactions at all. Secondly as i said this works without transaction block).
This piece of code works fine if taken out from Transaction Block.
ProductLocation.SaveLocation is creating Repository for Location . Here is the code from Save Location.
IRepository<LocationInfo> locRepo= new GenericRepository<ProductOrganizationEntity, LocationInfo>(new ProductOrganizationEntity());
if (loc.locID <= 0) // This is a new Location to be added.
locRepo.Add(locEntity);
else
locRepo.Attach(siteToAdd);
locRepo.SaveChanges();
Here is what i have done in my generic repository for thse methods
public void Attach(TEntity entity)
{
if (entity == null)
throw new ArgumentException("Update : Supplied Entity is Null.");
_currentDbSet.Add(entity);
System.Data.Entity.Infrastructure.DbEntityEntry entry = _dataContext.Entry(entity);
entry.State = System.Data.EntityState.Modified;
}
and this is what i have in SaveChanges in my generic repo.
public virtual void SaveChanges()
{
if (_dataContext == null)
throw new Exception("SaveChanges: DataContext is not initialized.");
_dataContext.SaveChanges();
}
What is that i am doing wrong here .
I appreciate any pointers.
It might be possible that your server is linked to another SQL server at the database level.
Perhaps look at this: http://msdn.microsoft.com/en-us/library/ms188279.aspx
Must admit I've never used linked servers (not yet at least), but seeing "Linked Servers" in the error made me think of this.