Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:41:13+00:00 2026-06-10T05:41:13+00:00

I’m using Symfony 2.1 forms with PropelBundle and I’m trying to refactor a form

  • 0

I’m using Symfony 2.1 forms with PropelBundle and I’m trying to refactor a form that had a drop-down list of objects (to select from) to instead use a jquery autocomplete field (working with AJAX). For the dropdown list I was using the following code (which worked perfectly for the drop-down) in my form type:

$builder->add('books', 'collection', array(
    'type'          => 'model',
    'options'       => array(
        'class'     => 'MyVendor\MyBundle\Model\Book',
        'property'  => 'title',
    ),
    'allow_add'     => true,
    'allow_delete'  => true,
    'by_reference'  => false,
    'required'      => false,
));

For the sake of giving a little context, let’s say we are creating a new “Reader” object and that we would like to select the Reader’s favorite books from a list of available “Book” objects. A collection type is used so that many “favorite books” can be selected in the new “Reader” form. Now, I would like to change the above to use autocomplete. For doing so, I tried to implement a Data Transformer to be able to get a Book object from a simple text field that could be used for the Autocomplete function to pass the Book ID as indicated in the answer to this Question. However, I was not able to figure out how to make the Data Transformer work with a collection type and Propel classes. I created a BookToIdTransformer class as indicated in the Symfony Cookbook and tried the following in the “ReaderType” file:

$transformer = new BookToIdTransformer();
$builder->add(
        $builder->create('books', 'collection', array(
            'type'          => 'text',
            'allow_add'     => true,
            'allow_delete'  => true,
            'by_reference'  => false,
            'required'      => false,
        ))->addModelTransformer($transformer)
);

With the above, I get a “Call to undefined method: getId” exception (apparently the Transformer expects a PropelCollection of Books, not a single Book object..). Does anyone know how to go about it? or let me know if there are other ways to implement the autocomplete in Symfony using Propel and allowing for selecting multiple objects (e.g. a collection of books)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:41:15+00:00Added an answer on June 10, 2026 at 5:41 am

    The solution I ultimately went for is slightly different from my previous answer. I ended up using a “text” field type instead of a “collection” field type in my “ReaderType” form, since I ended up using the Loopj Tokeninput jQuery plugin which allows for selecting multiple objects (e.g. “Favorite Book”) to associate to my main object (e.g. “Reader” object) in the form. Considering that, I created a “Data Transformer” for transforming the objects’ ids passed (in a comma separated list in the text field) into a Propel Object Collection. The code is shared as follows, including a sample ajax object controller.

    The key part of the “ReaderType” form looks as follows:

    $transformer = new BooksToIdsTransformer();
    $builder->add(
        $builder->create('books', 'text', array(
            'required' => false,
        ))->addModelTransformer($transformer)
    );
    

    The “Data Transformer” file looks like this:

    // src/MyVendor/MyBundle/Form/DataTransformer/BooksToIdsTransformer.php
    namespace MyVendor\MyBundle\Form\DataTransformer;
    
    use \PropelObjectCollection;
    use Symfony\Component\Form\DataTransformerInterface;
    use Symfony\Component\Form\Exception\UnexpectedTypeException;
    use MyVendor\MyBundle\Model\BookQuery;
    
    class BooksToIdsTransformer implements DataTransformerInterface
    {
        public function transform($books)
        {
            if (null === $books) {
                return "";
            }
    
            if (!$books instanceof PropelObjectCollection) {
                throw new UnexpectedTypeException($books, '\PropelObjectCollection');
            }
            $idsArray = array();
            foreach ($books as $book) {
                $idsArray[] = $book->getId();
            }
            $ids = implode(",", $idsArray);
            return $ids;
        }
    
        public function reverseTransform($ids)
        {
            $books = new PropelObjectCollection();
    
            if ('' === $ids || null === $ids) {
                return $books;
            }
    
            if (!is_string($ids)) {
                throw new UnexpectedTypeException($ids, 'string');
            }
            $idsArray = explode(",", $ids);
            $idsArray = array_filter ($idsArray, 'is_numeric');
            foreach ($idsArray as $id) {
                $books->append(BookQuery::create()->findOneById($id));
            }
            return $books;
        }
    }
    

    The ajax controller that returns a json collection of “books” to the Tokeninput autocomplete function is as follows:

    namespace MyVendor\MyBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use MyVendor\MyBundle\Model\BookQuery;
    
    
    class ClassAjaxController extends Controller
    {
        public function bookAction(Request $request)
        {
            $value = $request->get('q');
    
            $books = BookQuery::create()->findByName('%'.$value.'%');
    
            $json = array();
            foreach ($books as $book) {
                $json[] = array(
                    'id' => $book->getId(),
                    'name' => $book->getName()
                );
            }
    
            $response = new Response();
            $response->setContent(json_encode($json));
    
            return $response;
        }
    }
    

    And finally, the router in the “routing.yml” file:

    ajax_book:
        pattern:  /ajax_book
        defaults: { _controller: MySiteBundle:ClassAjax:book }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.