0

I have followed this: http://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_sortable_listing.html

And when trying to list my items I get this:

PHP Fatal error:  Call to a member function getLastPosition() on a
non-object in
/home/tirengarfio/workspace/javierperezpla/src/Placas/FrontendBundle/Admin/ColumnaAdmin.php
on line 45, referer:
http://javierperezpla/app_dev.php/admin/placas/frontend/columna/3/edit

This is my code:

namespace Placas\FrontendBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class ColumnaAdmin extends Admin
{
    public $last_position = 0;

    private $container;
    private $positionService;

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('name', 'text', array('label' => 'Nombre', 'required' => false))
            ->add('position', 'text', array('label' => 'Posición', 'required' => false))
        ;
    }

     public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container)
     {

         $this->container = $container;
     }

     public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
     {
  /*It is not entering here*/
         $this->positionService = $positionHandler;
     }

     protected $datagridValues = array(
         '_page' => 1,
         '_sort_order' => 'ASC',
         '_sort_by' => 'position',
     );

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        /*var_dump($this->postionService);die; HERE I GET NULL*/
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
        $listMapper
            ->addIdentifier('name')
            ->add('enabled')
            ->add('_action', 'actions', array(
                'actions' => array(
                'move' => array('template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig'),
        )));
    }

    protected function configureRoutes(\Sonata\AdminBundle\Route\RouteCollection $collection)
    {
        $collection->add('move', $this->getRouterIdParameter() . '/move/{position}');
    }
}


<?php

namespace Placas\FrontendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 */
class Columna
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, name="name")
     *
     * @var string $name
     */
    protected $name;

    /**
     * @Gedmo\SortablePosition
     * @ORM\Column(name="position", type="integer")
     */
    private $position;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Column
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }


    /**
     * Set position
     *
     * @param integer $position
     * @return Columna
     */
    public function setPosition($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * Get position
     *
     * @return integer 
     */
    public function getPosition()
    {
        return $this->position;
    }

sonata.admin.columna:
    class: Placas\FrontendBundle\Admin\ColumnaAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: "Contenido web", label: "Columna" }
    arguments:
        - ~
        - Placas\FrontendBundle\Entity\Columna
        - 'PixSortableBehaviorBundle:SortableAdmin' # define the new controller via the third argument
    calls:
        - [ setTranslationDomain, [PlacasFrontendBundle]]
tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • 1
    Did you add calls for `setPositionService` in your service definition for `ColumnaAdmin`? Please show me your definition. BTW. You should not inject whole `container` if you don't really need it! – NHG Feb 11 '14 at 16:26
  • @NGH do you mean the admin.yml? I have just pasted it in my question. – tirenweb Feb 11 '14 at 16:56
  • I don't see it in your question. Do you mean `admin.yml` from documentatnion? Please provide me configuration for `ColumnaAdmin` in your application : ) – NHG Feb 11 '14 at 17:10
  • NGH, sorry I forgot to paste it, now it is pasted – tirenweb Feb 11 '14 at 17:31

1 Answers1

0

You should call setPositionService():

sonata.admin.columna:
    class: Placas\FrontendBundle\Admin\ColumnaAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: "Contenido web", label: "Columna" }
    arguments:
        - ~
        - Placas\FrontendBundle\Entity\Columna
        - 'PixSortableBehaviorBundle:SortableAdmin' # define the new controller via the third argument
    calls:
        - [ setTranslationDomain, [PlacasFrontendBundle]]
        - [ setPositionService, [@pix_sortable_behavior.position]]

Where pix_sortable_behavior.position must be instance of \Pix\SortableBehaviorBundle\Services\PositionHandler.

In fact in documentation that you provided is:

And in the admin.yml add the following call

 - [ setContainer, [ @service_container ] ]
 - [ setPositionService, [@pix_sortable_behavior.position]]
NHG
  • 5,807
  • 6
  • 34
  • 45