![]() |
|
Snippets |
|
When you use the object_select_tag you specify related_class=Author for example. This will return all Authors and use the __toString() method to display the author.
However how do you select a subset of Authors based on some criteria.
To do this you can create a new Peer class that extends your BaseAuthorPeer, for example LiveAuthorPeer.
<?php class LiveAuthorPeer extends BaseAuthorPeer { public static function doSelect(Criteria $criteria, $con = null) { $criteria = new Criteria(); $criteria->add(AuthorPeer::IS_LIVE, 1); return AuthorPeer::doSelect($criteria); } } ?>
You can the call this new class from you object_select_tag and it will return select options for only those authors with the value of IS_LIVE = 1.
<?php echo object_select_tag($books, 'getAuthorId', array ( 'related_class' => 'LiveAuthors' )) ?>
Comments on this snippet
nice - took me a while to realise the significance but you've found a very clean way workaround that still allows the use the symfony helper function. Very nice work!
In this case, why don't you use a simple
select_tag()?In your action, you can add:
Then your template can simply show:
The
object_select_tag()is good when you don't want to bother about the options and let symfony do the job. If you want your own array of options, then use the regularselect_tag()to achieve full control.That was my 2 Eurocents.
excellent
hi, i have a problem... i did try to apply the code but, i couldnt do that. first, i add the peer class SubMedioWPeer in the file named SubMedioPeer.php and doesnt work second, i created a file named SubMedioWPeer.php with the class and doesnt work
when i try to call the related_class i put
<?php echo object_select_tag($noticias, 'getSubTipoMedio', array ('related_class' => 'SubMedioW')) ?>
if anybody can help me i will appreciate, sorry for my english
tried both ways.
New Peer class: + don't have to modify the template file to keep it working + put default selection as well - logic in model layer, I'm unable to pass parameters there.
select_tag approach: + able to control parameters from action - didnt make it working (I'm using the action/template generated by AdminGenerator and then manually updated)
Don't forget to clear the cache to see changes - spent 2 hours here :)
For #2,
doesn't it have to be called:
foreach ($author as $authors) { $options[$author->getId()] = $author->getName(); }
?
Have an attention to the changed author / authors in the foreach.
Sorry, i forgot the code tags. Maybe someone could add so it doesn't looks so crappy.
Thanks in advance.