Reference the following code:
Code: Select all
abstract class BaseState extends BaseObject implements Persistent {
// code
}
class State extends BaseState {
// code
}
Code: Select all
$c = new Criteria();
$c->add(StatePeer::NAME, 'Florida');
$state = StatePeer::doSelectOne();
Code: Select all
class CasesByState extends State {
public function getCaseCount($year = null) {
// get the number of cases in this state
}
}
Code: Select all
# create db criteria
$query = new Criteria();
$query->add(StatePeer::NAME, 'Florida');
# get an instance of State
$state = StatePeer::doSelectOne($c);
# create instance of CasesByState
$cases = new CasesByState();
# copy State into CasesByState
$state->copyInto($cases);
Here is the code that populates an object from a PDO Statement in BaseStatePeer. StatePeer extends BaseStatePeer.
Code: Select all
/**
* The returned array will contain objects of the default type or
* objects that inherit from the default.
*
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
*/
public static function populateObjects(PDOStatement $stmt)
{
$results = array();
// set the class once to avoid overhead in the loop
$cls = StatePeer::getOMClass();
$cls = substr('.'.$cls, strrpos('.'.$cls, '.') + 1);
// populate the object(s)
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$key = StatePeer::getPrimaryKeyHashFromRow($row, 0);
if (null !== ($obj = StatePeer::getInstanceFromPool($key))) {
$results[] = $obj;
} else {
$obj = new $cls();
$obj->hydrate($row);
$results[] = $obj;
StatePeer::addInstanceToPool($obj, $key);
} // if key exists
}
$stmt->closeCursor();
return $results;
}