Random stuff and high class geekery by a web developer studio edlington
6 Mar

Update primary key in Propel PHP, Propel ORM

Have you ever tried to change an object’s primary key in Propel? Well…it’s not as simple like call setters and save(). The problem is that you’re changing the primary key in the object and then you’re asking the object to build the UPDATE SQL. I’m not sure what the correct approach is to this issue, but Propel is clearly not paritularly adept at updating primary keys.

So, the solution is to use the BasePeer::doUpdate() method.

 
$con = Propel::getConnection(ProductPeer::DATABASE_NAME);
$product = ProductPeer::retrieveByPK(10, 'en');
$selectCriteria = $product->buildPkeyCriteria();
// update values are also stored in Criteria object
$product->setId(12);
$product->setLang('en');
$product->setParam('a');
$updateValues = $product->buildCriteria();
BasePeer::doUpdate($selectCriteria, $updateValues, $con);
Comments
1/1