stablum Posted June 26, 2013 Share Posted June 26, 2013 (edited) So I have a few tables configured in Propel, with generated Peer static classes. This is the framework I have to use and I cannot change it. The problem is that I need to perform very similar search operation on different tables, that obviously have different Peer classes. This lead to duplicated code. Now, I am not such that great guru of PHP, so maybe there is some construct (avoiding eval) that might help me in this case; I really would like to avoid writing duplicated code that performs the same exact calls on just different static Peer classes. example from a method of a class I am writing: $criteria = new Criteria();$criteria->add(FoobarPeer::CONTRACTNR,$data['contractnr']);$result = FoobarPeer::doSelect($criteria);if(count($result) > 1){ throw new FoobarException("status: more than one row with the specified contractnr.");}if(count($result) == 0){ // no object with given contractnr. Create new one. $obj = $this->factory->createORM("foobar"); $obj->setCreatedAt(time());} else { // use and update existing object. $obj = $result[0];} (as you can see I managed to write a factory method for the row object, but obviously I cannot do this for static classes) Anyone got some nice solution? thanks P.S. sorry for my approximative english Edited June 26, 2013 by stablum Quote Link to comment https://forums.phpfreaks.com/topic/279586-propel-peer-static-classes-how-to-avoid-to-write-duplicated-code/ Share on other sites More sharing options...
ignace Posted June 26, 2013 Share Posted June 26, 2013 We need to see real code, and what you will duplicate, to tell you how you can keep it DRY. Quote Link to comment https://forums.phpfreaks.com/topic/279586-propel-peer-static-classes-how-to-avoid-to-write-duplicated-code/#findComment-1438041 Share on other sites More sharing options...
ignace Posted June 26, 2013 Share Posted June 26, 2013 Also, read this: http://propelorm.org/blog/2010/08/03/refactoring-to-propel-1-5-from-peer-classes-to-query-classes.html Quote Link to comment https://forums.phpfreaks.com/topic/279586-propel-peer-static-classes-how-to-avoid-to-write-duplicated-code/#findComment-1438042 Share on other sites More sharing options...
stablum Posted June 27, 2013 Author Share Posted June 27, 2013 this is real code. Except for the fact that I replaced some names with "foobar". I cannot spread my company's code like it's open source. What will be duplicated, as previously stated, is the calls to methods in the Peer class, but performed on a different Peer class. Unfortunately I cannot use Query classes; I have to stick on Peer classes. Quote Link to comment https://forums.phpfreaks.com/topic/279586-propel-peer-static-classes-how-to-avoid-to-write-duplicated-code/#findComment-1438171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.