Zend Framework findBy魔术方法?

Sea*_*gen 2 php zend-framework

而不是$this->fetchAll('email = ?',$email)->current()在模型类中使用,有没有办法做$this->fetchByEmail($email)$this->findByEmail($email)

已经有一个像这样的神奇方法Zend_Log,而不是$myLogger->log("Something went wrong",Zend_Log::CRIT)你只是编写$myLogger->crit("Something went wrong")并自动映射(通过方法中的一些时髦的反射__call()).

有没有人知道在任何Zend_Db课程中是否有类似的东西,或者我是否必须为我做一些事情来做这件事?

Noa*_*ich 5

对于您想要的特定功能,您需要构建自定义功能.老实说,魔法__call()函数背后的逻辑并不是那么困难.

像这样的东西应该做的伎俩:

public function __call($function, $args)
{
    // Expects findBy to be the first part of the function
    $criteria = substr($function, 6);
    $criteria = strtolower($criteria);

    $select = $this->select()
                ->from($this->_name)
                ->where($criteria . ' = ?', $args);
}
Run Code Online (Sandbox Code Playgroud)

显然,如果您希望它处理更复杂的情况,如数组或多个条件参数,您需要实现更好的检查,但这应该提供基本的想法.