使用 Propel 选择条件选择某些字段

发布于 2024-09-28 04:26:56 字数 502 浏览 4 评论 0原文

我无法在 Symfony 1.4 中为 Propel Criteria 提供的文档中找到类似的内容

默认情况下,该标准是:

$this->Merchantss = MerchantsPeer::doSelect(new Criteria());

但是,这会选择“商家”表中的所有字段。我只想选择几个,比如:id、名称、类别。如何通过标准来做到这一点?

我尝试了以下操作,但它没有返回输出:

$criteria = new Criteria();
$criteria->add(MerchantsPeer::NAME);
$criteria->add(MerchantsPeer::ID);
$criteria->add(MerchantsPeer::CATEGORY);
$this->Merchantss = MerchantsPeer::doSelect($criteria);

提前致谢

I am unable to find something like this in documentation provided for Propel Criteria in Symfony 1.4

The criteria, by default, is:

$this->Merchantss = MerchantsPeer::doSelect(new Criteria());

However, this selects all the fields in the table for 'Merchants'. I would only like to select a couple, lets say: id, name, category. How is it possible to do so through criteria?

I tried the following but it doesn't return an output:

$criteria = new Criteria();
$criteria->add(MerchantsPeer::NAME);
$criteria->add(MerchantsPeer::ID);
$criteria->add(MerchantsPeer::CATEGORY);
$this->Merchantss = MerchantsPeer::doSelect($criteria);

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2024-10-05 04:26:56

操作方法如下:

$criteria = new Criteria();
$criteria->clearSelectColumns();
$criteria->addSelectColumn(MerchantsPeer::NAME);
$criteria->addSelectColumn(MerchantsPeer::ID);
$criteria->addSelectColumn(MerchantsPeer::CATEGORY);
$this->MerchantsStmt = MerchantsPeer::doSelectStmt($criteria);

$this->MerchantsStmt 是一个 PDOStatement 对象,可以使用 ->fetch() 方法对其进行迭代。更多详情请参见这里: PDOStatement

为了显示模板中的内容,您需要知道 symfony 会“保护”传递给模板的对象内容,以防止恶意代码被执行。如果您信任 $MerchantsStmt 对象的内容,那么您可以像这样迭代它:

<?php

$MerchantsStmt = $sf_data->getRaw('MerchantsStmt');

foreach ($MerchantsStmt->fetchAll() as $value)
{
  //some display logic
}

?>

This is how to do it:

$criteria = new Criteria();
$criteria->clearSelectColumns();
$criteria->addSelectColumn(MerchantsPeer::NAME);
$criteria->addSelectColumn(MerchantsPeer::ID);
$criteria->addSelectColumn(MerchantsPeer::CATEGORY);
$this->MerchantsStmt = MerchantsPeer::doSelectStmt($criteria);

$this->MerchantsStmt is a PDOStatement object, which can be iterated using the ->fetch() method. See here for more details: PDOStatement

In order to display the content in the template, you need to know that symfony 'protects' the content of the object passed to the template to prevent malicious code from being executed. If you trust the content of the $MerchantsStmt object, then you can iterate it like this:

<?php

$MerchantsStmt = $sf_data->getRaw('MerchantsStmt');

foreach ($MerchantsStmt->fetchAll() as $value)
{
  //some display logic
}

?>
ま昔日黯然 2024-10-05 04:26:56
just to make addition information:

from the solution:
$this->MerchantsStmt = MerchantsPeer::doSelectStmt($criteria);

instead:
$rs = MerchantsPeer::doSelectRS($criteria);

To Access:      
$rs->setFetchMode(ResultSet::FETCHMODE_ASSOC);
while ($rs->next())
{
    $name =  $rs->get('NAME'); 
}
just to make addition information:

from the solution:
$this->MerchantsStmt = MerchantsPeer::doSelectStmt($criteria);

instead:
$rs = MerchantsPeer::doSelectRS($criteria);

To Access:      
$rs->setFetchMode(ResultSet::FETCHMODE_ASSOC);
while ($rs->next())
{
    $name =  $rs->get('NAME'); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文