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.

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

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题