dongzheng3113 2014-09-24 09:11
浏览 20
已采纳

Zend - 无法识别的方法'current()'

I will make it as simple as possible...

I have two entities : Pds and Specialite

Here are the two DbTable class :

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

Here are the two Models :

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 ... ] 
}

And my 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(){} }

The link between Pds and Specialite is :

  • Pds have one or several speciality
  • Specialite concerns one or several Pds

I want to get the Specialite of a PDS. Here is index action in my controller :

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

But the application tell me thaht current() method is unrecognized ... I'm looking to make it work since yesterday but I don't see where is the problem...

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongpu2727 2014-09-24 10:36
    关注

    First have a look at http://akrabat.com/zend-framework/on-models-in-a-zend-framework-application/

    So you're doing several things wrongly.

    First Application_Model_Pds extends Zend_Db_Table_Row_Abstract which doesn't have a current method. (Only Zend_Db_Table_Rowset_Abstract has it)

    Tell the Application_Model_DbTable_Pds which $_rowClass to use:

    class Application_Model_DbTable_Pds extends Zend_Db_Table_Abstract
    {
        ...
    
        protected $_rowClass = 'Application_Model_Pds';
    }
    

    In this way you don't have to pass it to the mapper:

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

    Also you don't need the properties for the db fields. They should be created automatically with the names from 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;
        }
    
        ...
    }
    

    In the controller:

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

    Note: Not tested! And I'm not sure if the relationships work. Have a look here for more info.

    Personally I like it more to have an independent model class like described here. It seems you tried to mix this concept with the Zend_Db_Table_Row_Abstract as model concept.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。