0
votes

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

2

2 Answers

3
votes

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
}

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