在Zend模型中使用$ this

时间:2014-12-26 21:22:22

标签: php zend-framework

我试图在我的Zend模型中编写getFunction()

只有作品:

public function getFunction($id){
    $Wiadomosc = new Application_Model_DbTable_Wiadomosc();
    $nieodczytane = $Wiadomosc->select();
    $nieodczytane->from(etc.....)
}

但我不想在模型中创建对象模型!

我想这样做:

public function getFunction($id){
    $nieodczytane = $this->select();
    $nieodczytane->from(etc.....)
}

,但..
Method "select" does not exist and was not trapped in __call()

怎么做?

2 个答案:

答案 0 :(得分:0)

我知道select()函数已经在Zend模型结构中定义,你的模型继承了它,所以就像这样:

public function getFunction($id){
  $nieodczytane = parent::select();
  $nieodczytane->from(etc.....)
}

答案 1 :(得分:0)

虽然你的问题写得很好而且不容易理解你想要实现的目标。让我试着回答一下。 如果您使用的是Zend DbTable。让我试着解释它是如何工作的。

例如,您有以下型号 //此类代表表格' Wiadomosc'在您的数据库方案

类Wiadomosc {

protected $_name = 'Wiadomosc';
protected $_primary = 'id';

} 如果你想写一个关于模型的get函数,通常用id和你查询 获取你想要的行,  你以Zend Framework的方式做它。

  /*** Get list. 
     * @param Integer $id
     * @return Array
     * @throws Exception if id could not be found.
     */
public function  getFunction($id)
{
    $id = (int)$id; //typcast 
    $row = $this->fetchRow('id = ' .$id);
    if(!$row)
    {
        throw new Exception("Could not find $id");
    }
    return $row->toArray();
}

如果情况并非如此,并且您从另一个模态查询,则可以尝试以下操作。

public function getFunction($id)
{

$select = $this->select();

$select->from(array('w' => 'Wiadomosc'), array('column1','column2', 'column3'));


$select->setIntegrityCheck(false);

if($rows = $this->fetchAll($select)){

    return $rows;
}elseif (empty($rows)){
    $rows = $this->_name->select();
    return $rows;
}

}

编辑:

您可以执行以下选项之一:

 public function getFunction($id){

            $nieodczytane = $this->select();
            $nieodczytane->from(array('t' => 'YourTableName'),array('*'));

            if($rows = $this->fetchAll($nieodczytane)){
                return $rows;
            }
        }




    public function getFunction($id){

            $nieodczytane = $this->select();

            if($rows = $this->fetchAll($nieodczytane)){
                return $rows;
            }
        }