waldo2188 / DatatableBundle

Symfony2 Ajax Datatable Bundle to simplify the use of http://datatables.net/ (and Doctrine entity inside)
MIT License
17 stars 16 forks source link

No results with eg code #6

Closed flopp26 closed 8 years ago

flopp26 commented 8 years ago

Hi,

Now it's works with symfony3, I get no results with my test.

the json is valid :

{"draw":1,"recordsTotal":"0","recordsFiltered":"0","data":[]}

If you have an idea why I get no results ;-)

thanks.

twig file is

<!-- include the assets -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.css"/>
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.js"></script>

{{ datatable({
        'js' : {
            'ajax' : path('datatable')
        }
    })
}}

My Controller:

class DatatableController extends Controller
{
    /**
     * set datatable configs
     * @return \Waldo\DatatableBundle\Util\Datatable
     */
    private function datatable()
    {
        return $this->get('datatable')
            ->setEntity("MyBundle:Task\Task", "x")// replace "XXXMyBundle:Entity" by your entity
            ->setDatatableId('dta-offres')
            ->setFields(
                array(
                    "name" => 'x.name',                        // Declaration for fields:
                    "identifiant" => 'x.identifiant',                    //      "label" => "alias.field_attribute_for_dql"
                    "_identifier_" => 'x.id')                          // you have to put the identifier field without label. Do not replace the "_identifier_"
            )
            ->setWhere(                                                     // set your dql where statement
                'x.error = :error',
                array('error' => 0)
            )
            ->setOrder("x.dateCreated", "desc");                                // it's also possible to set the default order
    }

    /**
     * Grid action
     * @Route("/ajax-datable-reports", name="datatable")
     * @return Response
     */
    public function gridAction()
    {
        return $this->datatable()->execute();                                      // call the "execute" method in your grid action
    }

    /**
     * Lists all entities.
     * @Route("/list", name="datatable_list")
     * @return Response
     */
    public function indexAction()
    {
        $respository = $this->getDoctrine()->getRepository('MyBundle:Task\Task');
        $r = $respository->findAll();
        // this works correctly and I get 92 results

        $this->datatable();                                                   // call the datatable config initializer
        return $this->render('MyBundle:layouts/rapports:datatable.html.twig');                 // replace "XXXMyBundle:Module:index.html.twig" by yours
    }
}

My Entity

/**

namespace MyBundle\Entity\Task;

 * @ORM\Entity
 * @ORM\Table(name="task")
 * @ORM\Entity(repositoryClass="MyBundle\Repository\TaskRepository")
 */
class Task
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="boolean")
     */
    private $error;

    /**
     * @ORM\Column(type="string")
     */
    private $identifiant;

    /**
     * @ORM\Column(type="string")
     */
    private $data;

    /**
     * @ORM\Column(type="string")
     */
    private $message;

    /**
     * @ORM\Column(type="datetime")
     */
    private $dateCreated;

    public function __construct()
    {
        $this->dateCreated = new \DateTime();
    }

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

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

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

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * @param mixed $error
     */
    public function setError($error)
    {
        $this->error = $error;
    }

    /**
     * @return mixed
     */
    public function getIdentifiant()
    {
        return $this->identifiant;
    }

    /**
     * @param mixed $identifiant
     */
    public function setIdentifiant($identifiant)
    {
        $this->identifiant = $identifiant;
    }

    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param $data
     * @return $this
     */
    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param mixed $message
     */
    public function setMessage($message)
    {
        $this->message = $message;
    }

    /**
     * @return \DateTime
     */
    public function getDateCreated()
    {
        return $this->dateCreated;
    }

    /**
     * @param $dateCreated
     * @return $this
     */
    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;
        return $this;
    }
}
waldo2188 commented 8 years ago

Did you try by hand if your request return a result ?

Try the code below in your controller.

private function datatable()
    {
        $dt = $this->get('datatable')
            ->setEntity("MyBundle:Task\Task", "x")// replace "XXXMyBundle:Entity" by your entity
            ->setDatatableId('dta-offres')
            ->setFields(
                array(
                    "name" => 'x.name',                        // Declaration for fields:
                    "identifiant" => 'x.identifiant',                    //      "label" => "alias.field_attribute_for_dql"
                    "_identifier_" => 'x.id')                          // you have to put the identifier field without label. Do not replace the "_identifier_"
            )
            ->setWhere(                                                     // set your dql where statement
                'x.error = :error',
                array('error' => 0)
            )
            ->setOrder("x.dateCreated", "desc");                                // it's also possible to set the default order

      // Print the SQL made by the DatatableBundle
      var_dump($dt->getQueryBuilder()->getDoctrineQueryBuilder()->getQuery()->getSQL());
      exit;

     return $dt;
    }
flopp26 commented 8 years ago

Thanks, I get not results because the clause setWhere do not replace value.

"SELECT t0_.name AS name_0, t0_.identifiant AS identifiant_1, t0_.id AS id_2 FROM task t0_ WHERE t0_.error = ? ORDER BY t0_.date_created DESC"

waldo2188 commented 8 years ago

It's normal that the getSQL() function return the request without replacing the value. But did you try in an PhpMyAdmin interface, to execute the SQL with '0' replacing '?'

On my side I tried something similar, and it work well. So, I don't really see where come from this bug.

flopp26 commented 8 years ago

Yes it's not a bug :-) I'm silly, my colleague has redraw database... Sorry to have wasted your time...

waldo2188 commented 8 years ago

That's Ok ;) Now, you know how to solve similar issue.