dongsi073898 2010-02-04 13:55
浏览 18
已采纳

如何在我的班级中多次分解参数?

I show you firsly my class : as you can see I repeated several times $versionId param because all the method needs it. I am asking myself if there is a way to factorize it so there is lesser repetition.

<?php
class Admin_Model_Version
{
  private $_db;
  private $_versionId;
  private $_path;

  public function __construct()
  {

  }

  /**
  * Récupère l'objet de la table version.
  *
  * @return Zend_Db_Table
  */
  public function getDb()
  {
    if(!isset($this->_db))
    {
      $this->_db = new Admin_Model_DbTable_Version();
    }

    return $this->_db;
  }

  /**
  * Retourne toutes les versions d'un projet.
  *
  * @param integer $versionId
  */
  public function getVersions($projectId)
  {
    $db     = $this->getDb();
    $select = $db->select();

    return $select
    ->where("project_idproject = ?", $projectId)
    ->query()->fetchAll();
  }

  public function getVersionPath($versionId)
  {
    $root          = realpath(dirname($_SERVER['SCRIPT_FILENAME']) . "/../");
    $patrimonyName = $this->getPatrimonyRecordByVersionId($versionId)->name_patrimony;
    $projectName   = $this->getProjectRecordByVersionId($versionId)->name_project;
    $versionName   = $this->getVersionRecordByVersionId($versionId)->lab_version;

    return $root . "/data/projects/" . $patrimonyName . "/" . $projectName . "/" . $versionName . "/";
  }

  /**
  * Vérifie si la version possède un repertoire de travail.
  *
  * Tous les répertoires de travail sont dans le répertoire /data/projects/
  */
  public function hasVersionDirectory($versionId)
  {
    $versionPath = $this->getVersionPath($versionId);

    // Si le chemin n'existe pas retourner une exception.
    if(!realpath($versionPath))
    {
      throw new Exception("<b>Admin_Model_Version</b> " . __LINE__ . " : Le répertoire n'existe pas!");

      return false;
    }
    else
    {
      return true;
    }
  }

  public function getPatrimonyRecordByVersionId($versionId)
  {
    $db = $this->getDb();

    // setIntegrityCheck(false) is required for join
    $row = $db->select()->setIntegrityCheck(false);

    return $row->from(array('ve' => 'version'), array())
    ->join(array('pr' => 'project'), 've.project_idproject = pr.idproject', array())
    ->join(array('pa' => 'patrimony'), 'pa.idpatrimony = pr.patrimony_idpatrimony')
    ->where('ve.idversion = ?', $versionId)
    ->query()->fetchObject();
  }

  public function getProjectRecordByVersionId($versionId)
  {
    $db = $this->getDb();

    // setIntegrityCheck(false) is required for join
    $row = $db->select()->setIntegrityCheck(false);

    return $row->from(array('ve' => 'version'), array())
    ->join(array('pr' => 'project'), 've.project_idproject = pr.idproject')
    ->where('ve.idversion = ?', $versionId)
    ->query()->fetchObject();
  }

  public function getVersionRecordByVersionId($versionId)
  {
    $db = $this->getDb();

    // setIntegrityCheck(false) is required for join
    $row = $db->select();

    $row->where('idversion = ?', $versionId);

    return $row->query()->fetchObject();
  }

  /**
  * Crée le répertoire de travail avec comme chemin le nom du patrimoine
  * suivi du nom de projet et du nom de version.
  */
  public function createHome($versionId)
  {
    $path = $this->getVersionPath($versionId);
    Zend_Registry::get('firephp')->info($path);

    if(!mkdir($path, 0755, true))
    {
      throw new Exception(__METHOD__ . "can't create directory");
    }
  }

  public function hasDirectorySRC($versionId)
  {
    $path =
  }

  public function hasDirectoryHTML($versionId)
  {

  }

  public function hasDirectoryXML($versionId)
  {

  }

  public function hasDirectorySVG($versionId)
  {

  }
}

Thx

  • 写回答

1条回答 默认 最新

  • ds1379551 2010-02-04 13:59
    关注

    It's tempting to say "turn it into an object attribute", but that would be inappropriate here since the purpose of this class is to handle the database. It would probably be better to have getVersions() return an array of some other type of object which would encapsulate the version ID and have the various version-ID-related methods instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路