检索函数内的数据

时间:2014-06-23 12:23:50

标签: php cakephp-2.0

我试图通过在PHP函数中使用CAKEPHP查找来获取用户数据,基本上它是一个回调函数。

有没有办法在回调函数中获取数据?

这里我收到错误“在不在对象上下文中时使用$ this”

public function links($msg=null){

$final_message = preg_replace(array('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i'), array('<a href="$1" target="_blank">$1</a>',  '$1<a href="/getdatabyhash/$2">#$2</a>'), $msg);

$regex = '~(?i)@[a-z]+[ ][a-z]+~';
$replaced = preg_replace_callback($regex,function ($match) {

foreach($match as $stringmatch)
{
$username= preg_replace('/@/',"",$stringmatch);
$fetchuser=$this->User->find('all', array('conditions'=>
array("User.First_Name  LIKE" => "%$username%")));

if(sizeof($fetchuser)> 0)
{
    return "<a href='@'>@</a>";
}
else
{

}
   }

1 个答案:

答案 0 :(得分:0)

尝试

App::uses('ClassRegistry', 'Utility');
class whatever etc.{

    protected $User;

    public function __construct(){
         $this->User = ClassRegistry::init('User');
    } 

}