Reputation: 2231
I am trying to do what I thought would be a trivial task using Symfony 1.4 with the propel ORM. I've got a custom static class setup that I would like to use to return the id of a row by searching the name of the item.
Here's my function:
static public function getItemIdByName ( $name )
{
$criteria = new Criteria();
$criteria-> clearSelectColumns();
$criteria -> add (ItemsPeer::ITEM_NAME, $name, Criteria::LIKE );
$criteria -> addSelectColumn(ItemsPeer::ITEM_ID);
$criteria -> setLimit (1);
$itemID = ItemsPeer::doSelect( $criteria );
return $itemID;
}
Now when I return $itemID, I get the name of the item. However, I cannot get the item_id column at all. I've tried returning $itemID[1] and several other column indexes, but it says those do not exist. If I print_r($itemID), I see an array of every column in that table, the only two with values are item_id and item_name. However, I still can't find a way to return or access that variable.
Any suggestions?
Upvotes: 0
Views: 1008
Reputation: 1888
I think that the error is in position of clearSelectColumns
, but I can mistaken. Try to use doSelectStmt instead of doSelect:
$c = new Criteria();
$c->add(ItemsPeer::ITEM_NAME, $name, Criteria::LIKE);
$c->clearSelectColumns();
$c->addSelectColumn(ItemsPeer::ITEM_ID);
$c->setLimit(1);
$stmt = ItemsPeer::doSelectStmt($c);
return $stmt->fetch(PDO::FETCH_COLUMN); // $itemID
This should solve your problems. However, if you are using sf 1.4 and Propel >= 1.5, I'd suggest you to use Propel Query:
return ItemsQuery::create()
->filterByItemName($name . '%')
->select('ItemId')
->findOne();
Upvotes: 1