iamluc / GloomyPagerBundle

This pager/datagrid/crud has advanced filtering & sorting (Array & Entity/QueryBuilder) in addition of pagination
MIT License
10 stars 4 forks source link

Problem When I have Add new Record in CRUD using GloomyPageBundle #7

Closed kunjalpopat closed 11 years ago

kunjalpopat commented 11 years ago

I have download Gloomy Pager Bundle package and it worked well with Gloomy Pager, Datagrid, CRUD functionality, but when i will try to use Gloomy CRUD functionality in another bundle when i will try to add new record on it. giving following error creating new record using Gloomy CRUD.

An exception has been thrown during the rendering of a template ("Warning: array_merge(): Argument #1 is not an array in /MY_PROJECT/src/Gloomy/PagerBundle/Twig/CrudExtension.php line 79") in BloggerBlogBundle:Default:gcrud.html.twig at line 1.

Thanks Kunjal

iamluc commented 11 years ago

Could you provide the content of BloggerBlogBundle:Default:gcrud.html.twig ?

kunjalpopat commented 11 years ago

below are my BloggerBlogBundle:Default:gcrud.html.twig content {{ crud(crud) }}

iamluc commented 11 years ago

Mmmhh... that's strange...

the "crud()" function calls the method "renderCrud()" in file CrudExtension.php, which calls "render()" in the same file with only 2 arguments. So the $params argument must be an empty array and you should not have trouble with the array_merge.

have you the last version of the bundle ? It "{{ crud(crud) }}" the complete content of the template ? Could you provide the controller ?

kunjalpopat commented 11 years ago

Thanks iamluc

Bundle version : yes, i have download on 14-June Content : {{ crud(crud) }} Controller :

$em = $this->getDoctrine()->getEntityManager(); $entities = $em->getRepository('BloggerBlogBundle:Blog')->findAll(); return $this->render('BloggerBlogBundle:Default:gcrud.html.twig', array('crud' => $this->get('gloomy.crud')->factory('BloggerBlogBundle:Blog'), 'entities' => $entities ));

kunjalpopat commented 11 years ago

Hello iamluc

I have send you bundle version, content & controller code in lasts questions, that are actually emergency for me

Thanks Kunjal

iamluc commented 11 years ago

Hi,

In file /MY_PROJECT/src/Gloomy/PagerBundle/Twig/CrudExtension.php, could you try to replace this code in function render($crud, $block, $params = array())

if ($template->hasBlock($block)) {
    return $template->renderBlock($block, array_merge($params, array('crud' => $crud)));
} 

by

if ($template->hasBlock($block)) {

    if (!is_array($params)) {
        if (is_object($params)) {
            throw new \Exception('Params should be an array, but is object '.get_class($params));
        }
        else {
            throw new \Exception('Params should be an array, but is '.gettype($params).' containing '.$params);
        }
    }

    return $template->renderBlock($block, array_merge($params, array('crud' => $crud)));
}

and tell me which exception is raised ?

kunjalpopat commented 11 years ago

Given me following error when i have replace code..

An exception has been thrown during the rendering of a template ("Params should be an array, but is NULL containing ") in BloggerBlogBundle:Default:gcrud.html.twig at line 1.

iamluc commented 11 years ago

Ok, i have an idea. Have you create a Form Type for your entity : "Form/BlogType.php" ?

kunjalpopat commented 11 years ago

no

iamluc commented 11 years ago

You must do it. you can use this command : php app/console generate:doctrine:form BloggerBlogBundle:Blog

kunjalpopat commented 11 years ago

i have use this command but given me same error

An exception has been thrown during the rendering of a template ("Params should be an array, but is NULL containing ") in BloggerBlogBundle:Default:crud.html.twig at line 1.

iamluc commented 11 years ago

The file "Form/BlogType.php" has been generated ?

kunjalpopat commented 11 years ago

yes, Form/BlogType.php has been generated.

iamluc commented 11 years ago

New idea !

You don't use the CRUD object correctly. According to the README, it's

return $this->get('gloomy.crud')->factory('MyBundle:MyEntity')->handle();

You must call the handle() method, and it will return an array to give to your template. If you want to pass other arguments, you must add them in the same array

kunjalpopat commented 11 years ago

Thanks iamluc i have try your new idea replace below code and its working when i click on Add button.. return $this->render('BloggerBlogBundle:Default:crud.html.twig', $this->get('gloomy.crud')->factory('BloggerBlogBundle:Blog')->handle());

but when save data given me below error.........

Catchable Fatal Error: Argument 2 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::render() must be an array, object given, called in /MY_PROJECT/src/Blogger/BlogBundle/Controller/DefaultController.php on line 78 and defined in /MY_PROJECT/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 97

after i have try this ways return $this->render('BloggerBlogBundle:Default:crud.html.twig', array('crud' => $this->get('gloomy.crud')->factory('BloggerBlogBundle:Blog')->handle()));

but not working.

iamluc commented 11 years ago

Hi,

The handle() method returns an array or a Response object. If you use the Template() annotation, you can just do

return $this->get('gloomy.crud')->factory('BloggerBlogBundle:Blog')->handle();

But if you don't use annotation, you can do:

$crud = $this->get('gloomy.crud')->factory('BloggerBlogBundle:Blog')->handle();
if (!is_array($crud)) {
    return $crud;
}
return $this->render('BloggerBlogBundle:Default:crud.html.twig', $crud);
kunjalpopat commented 11 years ago

Hi iamluc

i have used your code & now its working.

Thankyou.