Skip to main content

Posts

Showing posts from October, 2011

Symfony2 choice list type for MongoDB backed forms

A couple of days ago, I had to implement a select field representing a one-to-one MongoDB relationship in one of my Symfony2 forms. Having spent some time reading the documentation, I decided to use the entity form field type for this purpose. class AcmeFormType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('parent', 'entity', array( 'class' => 'Acme\MyBundle\Document\MyDocument', 'property' => 'name', 'label' => 'Parent', 'empty_value' => 'Root' )); } } And I got presented with this exception: Class Acme\MyBundle\Document\MyDocument is not a valid entity or mapped super class. I realized Symfony2 was trying to load the necessary class using the default entity manager defined by the Doctrine ORM and failing utterly. Afte...

Integrating Memcached with Symfony2

Overview This is a short tutorial to add Memcached support to your Symfony 2 application. This is actually not a really good way to add caching support to your application as your API should support multiple caching mechanisms just like Zend_Cache does; however, all I need for my application is a way to access a Memcached singleton instance and this setup satisfies that requirement. Configuration Generate a new bundle in your application source directory using the command below. php app/console generate:bundle In your app/config.yml, create a section for your bundle: app/config/config.yml acme_memcached: servers: server1: host: localhost port: 11211 weight: 25 server2: host: localhost port: 11211 weight: 75 Note: You should also add a persistence flag to this configuration; however, I am going to omit this flag to keep this tutorial simple. Once we have a format that we like, we need t...

Symfony2 SecurityBundle and FOSUserBundle integration: How does it work?

Overview A couple of days ago, I realized I needed to add some new functionality to the login process. Specifically, I needed to track all previous login attempts. Not knowing anything about the new Symfony2 SecurityBundle, I had to go through the underlying code to understand what was going on. In the process, I think got a basic idea about how the new SecurityBundle interacts with FOSUserBundle. Configuration I have a basic security configuration as illustrated below. app/config/security.yml security: encoders: Symfony\Component\Security\Core\User\User: plaintext role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN providers: fos_userbundle: id: fos_user.user_manager firewalls: main: pattern: .* form_login: provider: fos_userbundle check_path: /user/login_check login_path: /user/login lo...

Unexpected token "name" of value "if" ("end of statement block" expected) in "WebProfilerBundle:Collector:logger.html.twig"

Encountered this WebProfilerBundle error message when I ran the bin/vendors script to update my Symfony2 bundles. Make sure your deps file is up to date; you need to pay special attention to your version values. In this case, update your twig version to v1.2.0 as illustrated below: [twig] git=http://github.com/fabpot/Twig.git version=v1.2.0 Run the vendors script to update your bundle and the error message should disappear. You can get the most up to date deps file from the symfony-standard repository located at: https://github.com/symfony/symfony-standard/blob/master/deps