How to display abstract list as checkboxes with form builder and initialize list from entity? (Symfony2)

96 views Asked by At

I have the form with user fields. I want to display checkboxes list of languages. Checked languages must be saved to database.

So I have 2 entity classes: User and UserSiteLanguage. See below ORM and Entity classes for them. 'fos_user_user' table doesn't contains any real fields for languages. And 'user_site_language' is a table that contains a binding selected languages to users.

User.orm.xml

<entity name="UserBundle\Entity\User" table="fos_user_user">

        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>
        <!-- Profile fields -->
        ...skipped ...

        <one-to-many field="siteEnabledLanguages" target-entity="UserBundle\Entity\UserSiteLanguage" mapped-by="user">
            <cascade>
                <cascade-all/>
            </cascade>
        </one-to-many>
    </entity>

UserSiteLanguage.orm.xml

<entity name="UserBundle\Entity\UserSiteLanguage" 
        table="user_site_language">

    <unique-constraints>
        <unique-constraint name="user_site_lang_locale_idx" columns="user_id,locale" />
    </unique-constraints>

    <lifecycle-callbacks>
        <lifecycle-callback type="prePersist" method="prePersist" />
        <lifecycle-callback type="preUpdate" method="preUpdate" />
    </lifecycle-callbacks>

    <id name="id" type="integer" column="id">
        <generator strategy="AUTO"/>
    </id>

    <field name="locale" column="locale" type="string" length="5" />       

    <many-to-one field="user" target-entity="UserBundle\Entity\User" inversed-by="siteEnabledLanguages">
        <join-column name="user_id" referenced-column-name="id" />
    </many-to-one>

</entity>

User.php

class User extends BaseUser{
        private $siteEnabledLanguages;

        /**
         * Add siteEnabledLanguage
         *
         * @param \UserBundle\Entity\UserSiteLanguage $siteEnabledLanguage
         *
         * @return User
         */
        public function addSiteEnabledLanguage(\UserBundle\Entity\UserSiteLanguage $siteEnabledLanguage)
        {
            $this->siteEnabledLanguages[] = $siteEnabledLanguage;

            return $this;
        }

        /**
         * Remove siteEnabledLanguage
         *
         * @param \UserBundle\Entity\UserSiteLanguage $siteEnabledLanguage
         */
        public function removeSiteEnabledLanguage(\UserBundle\Entity\UserSiteLanguage $siteEnabledLanguage)
        {
            $this->siteEnabledLanguages->removeElement($siteEnabledLanguage);
        }

        /**
         * Get siteEnabledLanguages
         */
        public function getSiteEnabledLanguages()
        {
            return $this->siteEnabledLanguages; 
        }
}

UserSiteLanguage.php

class UserSiteLanguage
{
    private $locale;
    private $id;
    private $user;

    // getters, setters
}

class AbstractUserFormType extends AbstractType

{
...
      ->add('siteEnabledLanguages', 'entity', array(
           'choices' => $this->getLocaleList(),
           'class' => 'UserBundle:UserSiteLanguage',
           'property' => 'locale',
           'multiple' => true,
           'expanded' => true,
     ))

  protected function getLocaleList()
  {
      $localesList = array();
      $locales     = $this->container->getParameter('lexik_translation.managed_locales');

      foreach ($locales as $locale) {
          $localeName = Intl::getLocaleBundle()->getLocaleName($locale);
          $localesList[$locale] = $localeName;
      }

      return $localesList;
  }
}

On my FormType I try to display locales list using 'entity' field type. I get locale list from config file. When I use this FormType, I get error: 'Expected argument of type "object or array", "string" given'.

Which could be the problem?

0

There are 0 answers