This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » General discussion » [solved]how can I show single record without foreach ?
[solved]how can I show single record without foreach ? [message #72225] Thu, 12 February 2009 14:25 Go to next message
dziobacz  is currently offline dziobacz
Messages: 86
Registered: February 2009
Member
I am new in symfony. I have in BooksTable:
 public function getBookInformation($id)
  {
	$q = Doctrine_Query::create()
	  ->select('k.*')
	  ->from('Books k')
	  ->where('k.id = ?', $id);
	 
    return $q->execute();
  }


so it can be only ONE RECORD.

In action.class I have:
$this->book = Doctrine::getTable('Books')->getBookInformation($request->getParameter('id'));


In template I must make:
<?php foreach ($book as $b): ?>
<?php echo $b->gettitle() ?>
<?php echo $b->getprice() ?>
<?php endforeach;?>


Why loop ? Loop is when I have many records and here I have only 1 record. Can I show title and price of book without foreach ?

[Updated on: Thu, 12 February 2009 16:54]

Re: how can I show single record without foreach ? [message #72226 is a reply to message #72225 ] Thu, 12 February 2009 14:30 Go to previous messageGo to next message
vjousse  is currently offline vjousse
Messages: 42
Registered: February 2009
Location: Le Mans - France
Member
Using find ?
$bookInformation = Doctrine::getTable('Books')->find($id);
return $bookInformation;



Then in your template $book will be a unique object

Or in your template

$b =$book[0];
echo $b->gettitle();
echo $b->getprice();


If you want to keep your method unchanged in your BookTable class
(checking if $book has at least one entry should be helpful too Wink )

[Updated on: Thu, 12 February 2009 14:40]


Freelance - http://fr.symfonians.net/person/vjousse
Re: how can I show single record without foreach ? [message #72227 is a reply to message #72226 ] Thu, 12 February 2009 14:36 Go to previous messageGo to next message
dziobacz  is currently offline dziobacz
Messages: 86
Registered: February 2009
Member
oh, yes !! thx xD

Could you also tell me - what kind of functions I can add in Books.class.php because I have Books.class.php and BooksTable.clas.php.........

[Updated on: Thu, 12 February 2009 14:40]

Re: how can I show single record without foreach ? [message #72240 is a reply to message #72227 ] Thu, 12 February 2009 16:30 Go to previous messageGo to next message
rschumacher  is currently offline rschumacher
Messages: 162
Registered: August 2007
Location: Switzerland
Senior Member
dziobacz wrote on Thu, 12 February 2009 14:36

Could you also tell me - what kind of functions I can add in Books.class.php because I have Books.class.php and BooksTable.clas.php.........


class BooksTable: contains functionality for the entirety of the books, i.e. not specific to an individual book. E.g. specific scenarios for searching books that you which to have at one single place. The methods in this class are typically static ones.

class Books: contains functionality that relates to the specific book, e.g. $book->getISBN(); that returns the ISBN code of this specific single book. Or: $book->getNumOrders($year = 2008); in which you may compute how many orders for this book have been placed in this year.

Cheers RAPHAEL
Re: how can I show single record without foreach ? [message #72242 is a reply to message #72240 ] Thu, 12 February 2009 16:48 Go to previous messageGo to next message
dziobacz  is currently offline dziobacz
Messages: 86
Registered: February 2009
Member
Thx you very much - now I understand Smile
Re: how can I show single record without foreach ? [message #72331 is a reply to message #72242 ] Fri, 13 February 2009 09:33 Go to previous messageGo to next message
trta  is currently offline trta
Messages: 10
Registered: January 2007
Junior Member
You can also use following:

public function getBookInformation($id)
  {
	$q = Doctrine_Query::create()
	  ->select('k.*')
	  ->from('Books k')
	  ->where('k.id = ?', $id);
	 
    return $q->fetchOne();
  }


Doctrine_Query::fetchOne method returns Doctrine_Record ancestor (= one column from your table) instead of Doctrine_Collection (= set of columns)

Re: [solved]how can I show single record without foreach ? [message #72333 is a reply to message #72225 ] Fri, 13 February 2009 10:09 Go to previous message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Btw, the Doctrine manual is *very* good - it is worth reading whole chapters at a time, and it will contain all of this stuff.


Remember Palestine
Previous Topic:format_date helper - ShortDatePattern Problem
Next Topic:Use Sf whitout PDO extansion
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software