0

I have a Message entity and a User one. I want to make users be able to send messages to others. I already can send and receive messages, everything is working fine, but in order to select the receiver of a message, a default drop-down with all the users is made.

What I want to do is change the drop-down to a text field and make an AJAX to the database and retrieve a list of users that are found matching the keyword inserted in the textfield. And when clicking upon one of them, the user is introduced in the textfield.

The problem that I am facing is that I don't know how to pass an entity instead of plaintext because I get this error

Argument 1 passed to PrivateMessageBundle\Entity\Message::setReceiver() must be an instance of CrudBundle\Entity\User, string given

Another question, is there a bundle or plugin that does this easier and I can modify? Or do I have to do it myself everytime for every ajax field?

I'm building the form like so

$builder
    ->add('title')
    ->add('content','textarea')
    ->add('receiver','text');

By default, receiver is a drop-down select and automatically selects the User entity, but once I change it to text it doesn't.

Here's also relevant parts of my entities, if you need them:

class Message
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=50)
     */
    protected $title;

    /**
     * @ORM\ManyToOne(targetEntity="CrudBundle\Entity\User")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $receiver;
    /**
     * @ORM\ManyToOne(targetEntity="CrudBundle\Entity\User")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $sender;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="string", length=2000)
     */
    protected $content;

And my User class, extending FOSUserBundle's BaseUser class.

class User extends BaseUser implements AuthorInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • 1
    see [this](http://stackoverflow.com/questions/23235080/is-it-possible-to-have-an-autocomplete-text-box-in-symfony-bootstrap) – user2268997 Sep 06 '15 at 09:41

1 Answers1

1

I think there are two ways to solve this problem.

1) add a custum formfield with the mapped = FALSE attribute.

->add('ajaxsearch', null, array('mapped' => false))

2) use a data-transformer.

Frank B
  • 3,667
  • 1
  • 16
  • 22
  • Using the data-transformer sounds like a lot of work just for a single field. Is there any good reason to use it over the unmapped field? – George Irimiciuc Sep 06 '15 at 10:39
  • Data-transformers a not as much work as you may think. The reason to use the unmapped field is that this field just shows (part of) usernames while it are not the names but the corresponding id's that you finally store into the database. When the form is submitted you can still read out the name that has been submitted, retrieve the corresponding user from the database and add the User entity to the Message entity through the addReceiver() method. – Frank B Sep 06 '15 at 12:12
  • By the way, if you want to use jQuery's autocomplete you might want to take a look to this recent question: http://stackoverflow.com/questions/32334289/adding-jquery-autocomplete-in-symfony2-entity-field/32341635#32341635 – Frank B Sep 06 '15 at 12:14