在 Symfony 1.4 中使用 Propel
我必须按玩家 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该避免在 Peer 类中添加逻辑,而应使用 ActiveQuery API。您可以阅读:http://www.propelorm.org/reference/model-criteria.html 和 http://propel.posterous.com/propel-query-by-示例。
对于您的情况,本节将为您提供帮助: http://www.propelorm.org /reference/model-criteria.html#creating_an_object_based_on_a_query。
然后,你可以写:
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: