2011-06-06 145 views
0

我有:数据库查询/模型

public function executeTest(sfWebRequest $request) 
    { 
      $query = Doctrine_Query::create() 
      ->from('Messages') 
      ->where('id = ?', $id); 
      $query->fetchArray(); 
    } 

我的确在LIB /型号功能:

 public function newfunction($id) 
    { 
      $query = Doctrine_Query::create() 
      ->from('Messages') 
      ->where('id = ?', $id); 

      return $query->fetchArray(); 

    } 

,现在有:

public function executeTest(sfWebRequest $request) 
    { 
     $this->newfunction($id); 
    } 

但我有错误:

sfException:调用未定义的方法消息操作:: newfunction。

如果:

public function executeTest(sfWebRequest $request) 
    { 
     $this->newfunction($id); 
    } 

我已经

Fatal error: Call to a member function newFunction() on a non-object in

我能做些什么?

回答

2

我假设你在lib/model/MessagesTable.class.php中创建了newFunction

$this->newFunction()将不起作用,因为$this引用当前操作实例,而不是您的模型的表。您需要使用两种:

Doctrine_Core::getTable("Messages")->newFunction($id); 

MessagesTable::getInstance()->newFunction($id); 

他们都做同样的事情,所以它只是一个偏好的问题。

此外,您不会在您的操作代码中为$id设置值。

+0

非常感谢帮助:) – clausix95 2011-06-06 16:16:47

1

我认为你的问题在这里是你为了使用非静态方法使用查询来获取对象和“this”引用是给你在sfAction实例:

public function executeTest(sfWebRequest $request) 
    { 
     $this->newfunction($id); 
    } 

我会建议更改newfunction声明模型类是这样的(创建这个功能可按在/lib/model/Doctrine/MessagesTable.class.php):

public static newfunction($id) 
{ 
    $query = Doctrine_Query::create() 
    ->from('Messages') 
    ->where('id = ?', $id); 

    return $query->fetchArray(); 
} 

现在你的行动应该是这个样子:

public function executeTest(sfWebRequest $request) 
    { 
    [..]//i'm assuming that id is set somewhere before this and is valid 
     MessagesTable::newfunction($id); 
    } 
+0

加入static并没有帮助。它是什么FunctionClass ::? – clausix95 2011-06-06 16:04:54

+0

您提到在lib/model中创建一个函数,但是您没有在巫师类中指定,所以我使用FunctionClass为了在创建该方法的巫婆中定义函数的类名称 – guiman 2011-06-06 16:07:04

+0

/lib/model/Doctrine/MessagesTable.class .php。 当我使$ this-> newfunction($ id);我有:调用未定义的方法planActions :: updatePlans。 if $ messages-> newfunction($ id);我有:注意:未定义变量: – clausix95 2011-06-06 16:15:12

0

为什么不利用教义提供的'魔法'?

这种查询可以是这样的:

Doctrine_Core::getTable("Messages")->findById($id); 

然后,你不会需要定义一个新的方法。 要查看更多内容,请看de文档页面的教条,进入de magic method findBy*