Saturday, December 28, 2013

SonataAdminBundle forms One to Many or Many to Many does not persist

This problem occurs due to the inverse-side object is not defined in the owning-side. In order to solve this, we have to manually specify it in the owning-side class. Suppose that we have a user class and a role class that are inverse-side and owning-side respectively. Here is the example of defining the inverse-side object.
//Acme/Bundle/DemeBundle/Entity\User
public function addRole(\Enstb\Bundle\VisplatBundle\Entity\Role $role)
{
    // Link each role with the user 
    // (This is important for SonataAdminBundle!!)
    $role->addUser($this);
    $this->roles->add($role);
    return $this;
}
public function removeRole(\Enstb\Bundle\VisplatBundle\Entity\Role $role)
{
    // Link each role with the user
    $role->removeUser($this);
    $this->roles->removeElement($role);
}
public function getRoles()
{
    return $this->roles;
}
The most important part is $role->addUser($this). It sends the object of Users class to be added in the Roles class. This makes the SonataAdminBundle to realize that which object should be linked together and persisted to a database.
And here is an example of addUser method in the Roles class:
//Acme/Bundle/DemeBundle/Entity\Role
public function addUser(\Enstb\Bundle\VisplatBundle\Entity\Users $user)
{
    $this->Users->add($user);
    return $this;
} 
Note: if you are not sure what is owning-side and inverse-side, read more about What is the difference between inversedBy and mappedBy?.

SonataAdminBundle - Making fields for two entities (Many to many relation)

If you always got the "Catchable Fatal Error: Object of class Acme\Bundle\DemoBundle\Entity\Roles could not be converted to string.", this tutorial will provide you a way to deal with it.
First of all, the error is returned because the __toString() class is not overridden. So, let make it! 
    //Acme\Bundle\DemoBundle\Entity\Roles
    function __toString()
    {
        return $this->getName();
    }
In order to make a checkbox of the roles available, we should specify true values for  expanded, compound, and multiple
   //Acme\Bundle\DemoBundle\Admin\UsersAdmin
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('Roles','sonata_type_model',array(
                  'expanded' => true,
                  'compound' => true,
                  'multiple' => true))

    ;    }

Saturday, December 21, 2013

Installing php54-xdebug on OSX.

In order to verify code coverage for PHPUnit test, it must need to install the php54-xdebug. In this tutorial, the Homebrew is used to install it.
First of all, we use the brew tap which allows you to add more Github repos to the list of formulas 
$ brew tap homebrew/dupes
Next, it's time to install the php54-xdebug! This should take for a while to install its dependencies. 
$ brew install php54-xdebug 
(Optional) If there is an error about the permission - cannot write  /usr/local/include, you can give it a permission by using:
$ sudo chown -R $USER /usr/local/include
Finally, add the following lines into php.ini
[XDebug]
; Only Zend OR (!) XDebug
zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
; XAMPP and XAMPP Lite 1.7.0 and later come with a bundled xdebug at /php/ext/php_xdebug.dll,without a version number.
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
; Port number must match debugger port number in NetBeans IDE Tools > Options > PHP
xdebug.remote_handler=dbgp
xdebug.var_display_max_depth = 10
xdebug.var_display_max_data = 2048
Note:  php54-xdebug is used to install PHP5.4. So, you can simply change the version as you need.