concretecms-community-store / community_store_shipping_example

A simple example shipping method for Community Store
MIT License
1 stars 0 forks source link

ORM Problems #1

Open JeRoNZ opened 7 years ago

JeRoNZ commented 7 years ago

I used this as an example (thanks) to build a shipping method. However, I found that repeated saves of the method through the dashboard lead to one record for each attempt.

The fix is to use the same ORM mechanism as used in the bundled community store methods, ie:

private function addOrUpdate($type, $data )
{
//////$em = Database::connection()->getEntityManager();
$em = \ORM::entityManager();
}
public function addMethodTypeMethod($data)
{
/////return $this->addOrUpdate('add', $data);
        return $this->addOrUpdate('update', $data);
}
Mesuva commented 7 years ago

Is this the same as what I was scratching my head on here? https://github.com/concrete5-community-store/community_store/issues/167

JeRoNZ commented 7 years ago

It sounds like exactly the issue I observed, yes. The two changes fixed it for me. I'm a bit of a noob at this ORM stuff, but it does seem to be the case that it's easy to detach or unmerge an object from ORM, such as might happen when creating a new instance of the entity manager, and that leads to a new record being inserted.

JeRoNZ commented 7 years ago

The code I ended up with is here: https://github.com/JeRoNZ/community_store_shipping_nz_flat_rate