views:

16

answers:

1

Hello,

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

A: 

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
}

?>
Raise
Thank you. I went with your solution and grabbed the whole array using ->fetchAll(). Now I am using foreach to traverse through it. How do I get specific values though? It seems like it is an object of type object(sfOutputEscaperArrayDecorator)
Aamir
Added a solution for iterating the content to the answer above.
Raise
Works superb. Learned something new today. Thank you! :-)
Aamir