开发者

Selecting certain fields using Propel select 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


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
}

?>


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'); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜