omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
259 stars 114 forks source link

ORM Adapter returns null for an attribute #51

Closed UtechtDustin closed 5 years ago

UtechtDustin commented 5 years ago

My Entity:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
 */
class Customer
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getDeactivated(): ?\DateTime
    {
        return $this->deactivated;
    }

    public function isDeactivated(): bool
    {
        return $this->deactivated !== null;
    }

    public function setDeactivated(\DateTime $deactivated): self
    {
        $this->deactivated = $deactivated;

        return $this;
    }
}

The datatable (php) Part:

$table = $this->createDataTable()
            ->add('name', TextColumn::class, [
                'className' => 'dt-body-center',
                'label' => $translator->trans('customerOverview.table.header.name')
            ])
            ->add('buttons', TwigColumn::class, [
                'className' => 'dt-body-center',
                'template' => 'customer/table/options.html.twig',
                'label' => $translator->trans('customerOverview.table.header.options')
            ])
            ->createAdapter(ORMAdapter::class, [
                'entity' => Customer::class,
            ])
            ->handleRequest($request);

I tried to show different buttons if the customer is (de)actived, but i always saw the deactived button so i used {{ dump(row) }} in the twig template and got the following output

Customer {#615 ▼
  -id: 1
  -name: "test123"
  -deactivated: null
}

But the Customer has a date in the database! Also if i fetch the User in Symfony the deativated attribute of the customer is filled!

Edit: Another quick question... is it possible to automaticlly translate the labels ?

UtechtDustin commented 5 years ago

Oh okay i figured out its null because i use no

->add('deactivated', DateTimeColumn::class)

But i f i i use it the null Value is a Datetime Object with the Date -001-11-30T00:00:00+01:00, also with the nullValue Option.

curry684 commented 5 years ago

I'm pretty sure you're seeing a different problem. The nullValue transformation is explicitly tested at https://github.com/omines/datatables-bundle/blob/master/tests/Unit/ColumnTest.php#L39 so I'm very sure that is working correctly.

What I think is that the Symfony property accessor is 'seeing' your isDeactivated method first and using it instead of getDeactivated. This would mean you're transforming false instead of null, which explains the date in 1BC. You should be able to solve this by explicitly binding to getDeactivated instead, or for example refactoring it to getDeactivationDate (will also help avoid issues in Twig).

UtechtDustin commented 5 years ago

@curry684 Im not sure why... but it works now with the same code... Maybe a doctrine caching issue ?

curry684 commented 5 years ago

That could also be possible 😄 glad to hear it's solved!