Symfony1 基于关系的信条查询与访问

Symfony1 基于关系的信条查询与访问,symfony1,doctrine,Symfony1,Doctrine,没问题,只是一个关于良好代码编写的问题。我还在学习Symfony+ORM,在这些框架中没有方向 我的数据库表中有User(带login列)和Account(也有login列,这与login in User不同) 一个用户(通过数据库ID和登录名标识)拥有多个帐户(也通过ID和作为帐户名的登录名标识)。因此schema.yml中的关系是: Account: (...) relations: idUser: class: User local: id_user forei

没问题,只是一个关于良好代码编写的问题。我还在学习Symfony+ORM,在这些框架中没有方向

我的数据库表中有User(带login列)和Account(也有login列,这与login in User不同)

一个用户(通过数据库ID和登录名标识)拥有多个帐户(也通过ID和作为帐户名的登录名标识)。因此schema.yml中的关系是:

Account:
(...)
relations:
  idUser:
    class: User
    local: id_user
    foreign: id_user
    foreignAlias: Accounts
现在,我试图通过以下方式访问与一个用户登录相关的所有帐户登录(假设我现在只显示当前用户的帐户登录列表):

/*$u=当前用户的登录名*/
$q=条令::getTable('User')->
createQuery('u')->innerjoin('u.login=?',$u的u.a帐户)->execute();
foreach($q[0]->帐户为$v){
echo$v->登录。“
”; }

这段代码运行得很好。然而,我现在想知道的是,这是不是丑陋的或不是最好的方式来实现这一点?正如我所说,我在Symfony中没有太多的定向功能,也不知道哪些编程方法是推荐的,哪些不是。

这对我来说并不糟糕,但我会这样写:

/* $login = login-name of the current user */
/* Always use ModelTable::getInstance() instead of Doctrine::getTable : 
   your IDE will give you better auto-completion if the doc block of the
   getInstance has a correct @return annotation.
   You will have all the methods of ModelTable in your auto-completion */
$users = UserTable::getInstance() 
  ->createQuery('u')
    ->innerjoin('u.Accounts a WITH u.login = ?', $login) 
      ->execute(); //Try to align opening parenthesis when writing DQL, it is easier to read

foreach ($users[0]->Accounts as $v) // egyptian brackets are for java(script) programmers
{
  echo $v->login . "<br />";
}
/*$login=当前用户的登录名*/
/*始终使用ModelTable::getInstance()而不是Doctrine::getTable:
您的IDE将为您提供更好的自动完成功能,如果
getInstance具有正确的@return注释。
您将在自动完成中拥有ModelTable的所有方法*/
$users=UserTable::getInstance()
->createQuery('u')
->innerjoin('u.login=?'的u.a帐户,$login)
->执行()//在编写DQL时,请尝试对齐左括号,这样更易于阅读
foreach($users[0]->Accounts as$v)//埃及括号用于java(脚本)程序员
{
echo$v->登录。“
”; }
/* $login = login-name of the current user */
/* Always use ModelTable::getInstance() instead of Doctrine::getTable : 
   your IDE will give you better auto-completion if the doc block of the
   getInstance has a correct @return annotation.
   You will have all the methods of ModelTable in your auto-completion */
$users = UserTable::getInstance() 
  ->createQuery('u')
    ->innerjoin('u.Accounts a WITH u.login = ?', $login) 
      ->execute(); //Try to align opening parenthesis when writing DQL, it is easier to read

foreach ($users[0]->Accounts as $v) // egyptian brackets are for java(script) programmers
{
  echo $v->login . "<br />";
}