Showing posts with label Doctrine2. Show all posts
Showing posts with label Doctrine2. Show all posts

Monday, June 11, 2012

Sonata AdminBundle: How to create a custom field

I wanted to create a custom field that is not in the database for the listFields so I searched the web how do it. Here is how I have done it:


class Entity 
{
    ...
    //custom getter
    public function getFullName()
    {
        return $this->getFirstName() . ' ' . $this->getLastName();
    }
}


In your EntityAdmin:

 protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
           ->add('fullName', 'doctrine_orm_string')
            ->add('_action', 'actions', array(
                'actions' => array(
                    'view' => array(),
                    'edit' => array(),
                    'delete' => array(),
                )
            ))
        ;
    }

Monday, May 7, 2012

How to check if Entity is in an array collection of another Entity

 Check if $friend is in the friends array collection of the user
$user->getFriends()->contains($friend); 

if $friend is in the array collection  method returns true otherwise it returns false

Sunday, May 6, 2012

Symfony2 many-to-many self referencing relation using Doctrine2

Symfony2 many-to-many self referencing relation using Doctrine2

 Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL

I created this blog because I wanted to help others who are self learners like me. I spent few hours to figure out why I was getting the next error when playing with Symfony2.
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL

I had an User entity a many to many self referencing relation. I still did not find out why I was getting the error but I found out here a way to get rid of the error. I just had to add the __sleep method for the user entity like this:

/**
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Repository\UserRepository")
 * @ORM\Table(name="users")
 * @ORM\HasLifecycleCallbacks()
 */

 class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    ..................
    public function __sleep()
    {
        return array('id');
    }
}

I hope this helped someone who got the same error as I did. If you have a better solution please post it here.