【问题标题】:Zend - Unrecognized method 'current()'Zend - 无法识别的方法'current()'
【发布时间】:2014-11-18 17:07:07
【问题描述】:

我会尽量简化...

我有两个实体:Pds 和 Specialite

这里有两个 DbTable 类:

class Application_Model_DbTable_Specialite extends Zend_Db_Table_Abstract
{
    /** 
    * @var $_name : Nom de la table dans la BDD
    * @var $_primary : Nom de la clé primaire de la table
    * @var $_schema : Nom de la BDD
    * @var $_adapter : Allias de la BDD dans le registre de Zend (défini dans le Bootstrap) 
    */
    protected $_name = 'ps_specialite';
    protected $_primary = 'ps_spe_id';
    protected $_schema = 'basename';
    protected $_adapter = 'db_1';

    protected $_referenceMap = array(
        'Pds' => array(
            'columns'           => 'numprat_id',
            'refTableClass'     => 'Pds',
            'refColumns'        => 'id'
        )
    );
}

class Application_Model_DbTable_Pds extends Zend_Db_Table_Abstract
{
    /** 
    * @var $_name : Nom de la table dans la BDD
    * @var $_primary : Nom de la clé primaire de la table
    * @var $_schema : Nom de la BDD
    * @var $_adapter : Allias de la BDD dans le registre de Zend (défini dans le Bootstrap) 
    */
    protected $_name = 'ps_praticiens';
    protected $_primary = 'numprat_id';
    protected $_schema = 'basename';
    protected $_adapter = 'db_1';
    protected $_dependentTables = array('Specialite');
}

这是两个模型:

class Application_Model_Specialite extends Zend_Db_Table_Row_Abstract {
    protected $_id;
    protected $_id_pds;
    protected $_principal;

    public function __construct(array $options = null){}

    public function __set($name, $value){}

    public function __get($name){}

    public function setOptions(array $options){}

    public function setId($id){}
    public function getId(){}

    public function setIdPds($id_pds){}
    public function getIdPds(){}

    public function setPrincipal($principal){}
    public function getPrincipal(){}    }

class Application_Model_Pds extends Zend_Db_Table_Row_Abstract {
    protected $_id;
    protected $_nom;
    protected $_nom_pro;
    protected $_prenom;

    [ ... same contruction as Specialite ... ] 
}

还有我的 PdsMapper.php :

class Application_Model_PdsMapper { protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Pds');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Pds $pds){}

    public function find($id, Application_Model_Pds $pds)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $pds->setId($row->id)
            ->setNom($row->nom)
            ->setPrenom($row->prenom);

        return $pds;
    }

    public function fetchAll(){} }

Pds 和 Specialite 之间的联系是:

  • Pd 具有一种或多种专长
  • Specialite 涉及一个或多个 Pds

我想获得 PDS 的 Specialite。这是我的控制器中的索引操作:

public function indexAction(){
    $o_mapper = new Application_Model_PdsMapper();
    $pds = $o_mapper->find('69000001', new Application_Model_Pds());
    $pds_69000001 = $pds->current();
    $specialiteByPds = $pds_69000001->findDependentRowset('Specialite');
    $this->view->pds = $pds;
    $this->view->specialite = $specialiteByPds; 
}

但应用程序告诉我 current() 方法无法识别......我希望从昨天开始让它工作,但我看不出问题出在哪里......

提前致谢

【问题讨论】:

  • 你在哪里实例化 $pdsRowset ?
  • 输入错误...这是 $pds,不是 $pdsRowset,抱歉。

标签: php zend-framework dependencies


【解决方案1】:

先看看http://akrabat.com/zend-framework/on-models-in-a-zend-framework-application/

所以你做错了几件事。

首先 Application_Model_Pds 扩展 Zend_Db_Table_Row_Abstract 没有 current 方法。 (只有Zend_Db_Table_Rowset_Abstract 有)

告诉Application_Model_DbTable_Pds 使用哪个$_rowClass

class Application_Model_DbTable_Pds extends Zend_Db_Table_Abstract
{
    ...

    protected $_rowClass = 'Application_Model_Pds';
}

这样你就不必将它传递给映射器:

public function find($id)
{
    $result = $this->getDbTable()->find($id);
    if (0 == count($result)) {
        // better return null or throw exception?
        return;
    }
    return $result->current();
}

您也不需要 db 字段的属性。它们应该使用 db 中的名称自动创建。

class Application_Model_Pds extends Zend_Db_Table_Row_Abstract
{
    public function setId($id)
    {
        $this->id = (int)$id;
    }

    public function getId()
    {
        return $this->id;
    }

    ...
}

在控制器中:

public function indexAction()
{
    $pds = $o_mapper->find('69000001');
    $specialiteByPds = $pds->findDependentRowset('Application_Model_DbTable_Specialite');
    $this->view->pds = $pds;
    $this->view->specialite = $specialiteByPds;
}

注意:未经测试!而且我不确定这些关系是否有效。查看here 了解更多信息。

我个人更喜欢拥有像here 描述的独立模型类。看来您试图将此概念与 Zend_Db_Table_Row_Abstract 混合为模型概念。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2021-11-21
    • 2016-04-19
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多