I want to use the prePersist() hook to set a fetched object into a ready-to-be-persisted object. But I can't figure how to use doctrine with Sonata Admin Bundle.
Here is my code :
namespace ShareBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class UserShareAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('quantity', 'text')
->add('user', 'sonata_type_model_list');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('quantity');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('quantity')->addIdentifier('user')->addIdentifier('date');
}
public function prePersist($object)
{
$shareManager = $this->getDoctrine()->getManager()->getRepository('ShareBundle:Share');
$value = $shareManager->findOneBy(array(), array('date' => 'DESC'));
$object->setShare($value);
}
}
Does anyone have any idea how to do it ?
Thanks !