Symfony 1.4原理模型对象数据检索问题 [英] Symfony 1.4 doctrine model object data retrieve issue

查看:95
本文介绍了Symfony 1.4原理模型对象数据检索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的symfony 1.4应用程序中,我使用的是原则数据模型。我是symfony和doctrine的新功能。在schema.yml文件中定义数据库表信息后,我从命令行生成了原理模型。我在Table.class.php文件中创建了一个自定义函数。以下是。

 类表扩展BaseTable 
{
public function getuname()
{
$ user = new Table();
$ uname = $ user-> getUsername();
return $ uname;
}

}

我想知道如何调用这里面的控制器?我称之为正常的MVC应用程序的方式。但是我不知道在symfony中是否正确。在symfony 1.4手册中,我找不到正确的方法。



这是我的控制器。

  class loginActions extends sfActions 
{

public function executeIndex(sfWebRequest $ request)
{
$ this-> userdata = User :: getuname();
}

}

然后我试图打印这里查看。

 <?php 
echo $ userdata;
?>

但视图显示的是空页。



更新异常详细信息--------------------------------



堆栈跟踪
at()
在SF_SYMFONY_LIB_DIR\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection.php行1082 ...

  $ message。= sprintf('。失败查询:%s',$ query); 

}


$ exc = new $ name($ message,(int)$ e-> getCode());

if(!isset($ e-> errorInfo)||!is_array($ e-> errorInfo)){

$ e-> errorInfo = array (null,null,null,null);

}


解决方案

使用 ...表类从数据库中检索对象的原则(在这种情况下,它将是一个 TableTable 类你可以使用它的方法从DB获取对象(例如 find($ id)),然后访问它们,所以在你的情况下,你的类应该是这样的: / p>

 类表扩展BaseTable 
{
public function getuname()
{
return $ this-> getUsername();
}
}

它实际上只是一个别名 getUsername()



然后在您的操作中:

  class loginActions extends sfActions 
{

public function executeIndex(sfWebRequest $ request)
{
$ user = Doctrine_Core :: getTable('Table') - > find(123);
$ this-> userdata = $ user-> getuname();
}
}

这将打印你的模板中的用户名(假设当然您有一个ID为$ code> 123 的用户)。


In my symfony 1.4 application i'm using doctrine data models.I'm new to symfony and doctrine.I generated doctrine models from command-line after defining database table information in the schema.yml file.Those generated successfully.Then i created a custom function inside Table.class.php file.Following is that.

class Table extends BaseTable
{
 public function getuname()
 {
    $user=new Table();
    $uname=$user->getUsername();
    return $uname;
 }

}

I want to know how to call this inside the controller ? I called it normal MVC application's way.But i don't know whether it's correct in symfony.In symfony 1.4 manual also i couldn't find a proper way to do this.

This is my controller.

class loginActions extends sfActions
{

  public function executeIndex(sfWebRequest $request)
  {
     $this->userdata = User::getuname();
  }

}

Then i tried to print this inside view.

<?php 
   echo $userdata;
?>

But view is showing an empty page.

Update with exception details--------------------------------

stack trace at () in SF_SYMFONY_LIB_DIR\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection.php line 1082 ...

    $message .= sprintf('. Failing Query: "%s"', $query);

    }


    $exc  = new $name($message, (int) $e->getCode());

    if ( ! isset($e->errorInfo) || ! is_array($e->errorInfo)) {

        $e->errorInfo = array(null, null, null, null);

    }

解决方案

When using Doctrine you retrieve objects from the database using the ...Table classes (in your case it will be a TableTable class. You can use its' methods to fetch objects from DB (e.g. find($id)) and then access them. So in your case your classes should something like this:

class Table extends BaseTable
{
    public function getuname()
    {
       return $this->getUsername();
    }
}

Now it effectively becomes just an alias of getUsername().

Then in your action:

class loginActions extends sfActions
{

    public function executeIndex(sfWebRequest $request)
    {
        $user = Doctrine_Core::getTable('Table')->find(123);
        $this->userdata = $user->getuname();
    }
}

This will print the username in your template (assuming of course that you have a user with id 123).

这篇关于Symfony 1.4原理模型对象数据检索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆