Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何列出某个作者在symfony中写的文章?_Php_Symfony1_Symfony 1.4_Backend - Fatal编程技术网

Php 如何列出某个作者在symfony中写的文章?

Php 如何列出某个作者在symfony中写的文章?,php,symfony1,symfony-1.4,backend,Php,Symfony1,Symfony 1.4,Backend,我在开发一个简单的新闻web应用程序 它包括表项目(id、标题、内容、用户id)和表用户(id、名称、用户名、密码..)等 在后端应用程序中,我只想列出具有特定用户id的文章 我将从$this->getUser()->getAttribute('user\u id')获得user\u id 据我所知,symfony网站上提供的文档并未涵盖此问题。将为您提供不同的方法 根据您的需要,您可以覆盖buildQuery: protected function buildQuery() { retur

我在开发一个简单的新闻web应用程序

它包括表项目(id、标题、内容、用户id)和表用户(id、名称、用户名、密码..)等

在后端应用程序中,我只想列出具有特定
用户id
的文章

我将从
$this->getUser()->getAttribute('user\u id')
获得
user\u id

据我所知,symfony网站上提供的文档并未涵盖此问题。

将为您提供不同的方法

根据您的需要,您可以覆盖
buildQuery

protected function buildQuery()
{
  return parent::buildQuery()
    ->where('user_id = ?', $this->getUser()->getAttribute('user_id'));
}
你可以:

  • 在generator.yml中,在like下插入新行:

    list:
      title: Articles list
      table_method: getArticlesByUserId
    
  • 在ArticleTable.class.php中插入一个新函数:

    public function getArticlesByUserId() {
      $userId = sfContext::getInstance()->getUser()->getAttribute( 'user_id' );
      $u = $this->createquery( 'u' )->where( 'u.user_id = ?', $userId );  
    
      return $q;
    }
    

在模型中使用它不是一个好主意。第二个答案是关于ArticlesTable.class的,它更适合我的特殊需要。我想显示作者的真实姓名和类别名称,而不是ID。它生成的查询要少得多,因为我使用了innerJoin-s,而不是向Article.class添加方法来填充提到的字段。谢谢。那么,您认为从示例ArticleTable.class.php中检索用户对象id的最佳方法是什么?