| [solved]how can I show single record without foreach ? [message #72225] |
Thu, 12 February 2009 14:25  |
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   |
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 )
[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   |
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: [solved]how can I show single record without foreach ? [message #72333 is a reply to message #72225 ] |
Fri, 13 February 2009 10:09  |
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
|
|
|