1
votes

I've gotta return the row by player Id, if there is no row, make one. This cant be the best way to do it.

class PlayerRafPeer extends BasePlayerRafPeer {

  /**
   * Returns a PlayerRaf object by playerId. 
   * @param int $player_id
   * @param PDO $con
   * @return PlayerRaf
   */
  public static function retrieveByPlayerId($player_id, $con = null)
  {
    if ($con === null) {
      $con = Propel::getConnection(self::DATABASE_NAME);
    }

    $criteria = new Criteria();
    $criteria->add(self::PLAYER_ID, $player_id);
    $v = self::doSelectOne($criteria, $con);

    if (!$v) {
        $player = new PlayerRaf();
        $player->setPlayerId($player_id)
             ->setEmailCount(0)
             ->setDate(date("Y-m-d"), time());
        self::doInsert($player, $con);
        return $player;
    } 
    return $v;
  }
1

1 Answers

1
votes

You should avoid to add logic in Peer classes, use the ActiveQuery API instead. You can read: http://www.propelorm.org/reference/model-criteria.html and http://propel.posterous.com/propel-query-by-example.

In your case, this section will help you: http://www.propelorm.org/reference/model-criteria.html#creating_an_object_based_on_a_query.

Then, you could write:

<?php

$player = PlayerRafQuery::create()
            ->filterById($id)
            ->findOneOrCreate();