Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

object_select_tag related classes

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'
)) ?>
by Alistair Stead on 2006-06-20, tagged forms  objects  propel 

Comments on this snippet

gravatar icon
#1 Stephen Beattie on 2006-06-20 at 06:08

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!

gravatar icon
#2 Francois Zaninotto on 2006-06-21 at 09:04

In this case, why don't you use a simple select_tag() ?

In your action, you can add:

$criteria = new Criteria();
$criteria->add(AuthorPeer::IS_LIVE, 1);
$authors = AuthorPeer::doSelect($criteria);
$options = array();
foreach ($authors as $author)
{
  $options[$author->getId()] = $author->getName();
}
asort($options);
$this->options = $options;

Then your template can simply show:

<?php echo select_tag('Authors', options_for_select($options)) ?>

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 regular select_tag() to achieve full control.

That was my 2 Eurocents.

gravatar icon
#3 Tim Batchelor on 2006-07-28 at 01:08

excellent

gravatar icon
#4 Ismael Molina on 2007-02-26 at 08:59

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

gravatar icon
#5 Vitezslav Zak on 2008-04-05 at 05:27

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 :)

gravatar icon
#6 symnooby on 2008-05-11 at 11:45

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.

gravatar icon
#7 symnooby on 2008-05-11 at 11:48

Sorry, i forgot the code tags. Maybe someone could add so it doesn't looks so crappy.

Thanks in advance.

You need to create an account or log in to post a comment or rate this snippet.