简体   繁体   English

选择特定的列Symfony 2 /主义

[英]Selecting specific columns Symfony 2 / Doctrine

I'm a Ruby on Rails programmer, but I have decided to start learning Symfony framework for PHP. 我是Ruby on Rails程序员,但是我决定开始学习PHP的Symfony框架。 And I've got a problem with Doctrine: 我对教义有一个问题:

There's a model of user: 有一个用户模型:

id | username | profession | password

In a database. 在数据库中。 I want to create a method that will: 我想创建一个将执行以下操作的方法:

SELECT DISTINCT profession FROM user;

So I wrote a code like this: 所以我写了这样的代码:

$connection = Doctrine_Manager::connection();
$query = 'SELECT DISTINCT profession FROM user';
$statement = $connection->execute($query);
$this->professions = $statement->fetch(PDO::FETCH_ASSOC);

But it's not working properly - it only retrieves the first one not all of the professions (should be 6). 但是它不能正常工作-它只检索第一个而不是所有专业(应该是6个)。

Could you help me please? 请问你能帮帮我吗? Is there any easier way to retrieve a single specific field from the table? 有没有更简单的方法从表中检索单个特定字段?

Fetch fetches the next column, use fetchAll if you want all at once. Fetch提取下一列,如果要一次全部使用fetchAll。

$statement->fetchAll(PDO::FETCH_ASSOC);

Or if you want to youse fetch and process one by one (if you don't want to store them in an array), 或者,如果您要逐个获取和处理(如果您不想将它们存储在数组中),

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
    echo $row['profession'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM