【问题标题】:Using Propel in Symfony 1.4在 Symfony 1.4 中使用 Propel
【发布时间】:2012-01-25 03:20:21
【问题描述】:

我必须按玩家 ID 返回行,如果没有行,则创建一个。 这不是最好的方法。

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;
  }

【问题讨论】:

    标签: symfony1 propel


    【解决方案1】:

    您应该避免在 Peer 类中添加逻辑,而是使用 ActiveQuery API。您可以阅读:http://www.propelorm.org/reference/model-criteria.htmlhttp://propel.posterous.com/propel-query-by-example

    在您的情况下,本节将为您提供帮助:http://www.propelorm.org/reference/model-criteria.html#creating_an_object_based_on_a_query

    那么,你可以这样写:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2011-11-07
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多