omines / datatables-bundle

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

association name expected error #156

Closed evskorobogatij closed 4 years ago

evskorobogatij commented 4 years ago

Hi!

I have 2 entity:

Petition:

class Petition
{
    use TimestampableEntity;
    use SoftDeleteableEntity;

    /**
     * @var UuidInterface
     * @ORM\Id
     * @ORM\Column(type="uuid")
     */
    private $id;

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

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity=People::class, inversedBy="my_petitions")
     * @ORM\JoinColumn(nullable=false)
     */
    private $autor;

People

class People
{

    use TimestampableEntity;
    use SoftDeleteableEntity;

    /**
     * @var UuidInterface
     * @ORM\Id
     * @ORM\Column(type="uuid")
     */
    private $id;

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

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

    /**
     * @ORM\Column(type="date")
     */
    private $birth_date;

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

Then in controller I try to out table on page:

   /**
     * @Route("/", name="admin_petition")
     */
    public function index(Request $request,DataTableFactory $dataTableFactory)
    {
        $table = $this->createDataTable()
            ->add("name",TextColumn::class)
            ->add("description",TextColumn::class)
            ->add("created_at",DateTimeColumn::class)
            ->add('author',TextColumn::class,['field' => 'people.lastname'])
            ->createAdapter(ORMAdapter::class,[
                'entity' => Petition::class,
            ])
            ->handleRequest($request);

        if ($table->isCallback()) {
            return $table->getResponse();
        }

        return $this->render('admin_petition/index.html.twig', [
            'datatable' => $table
        ]);
    }

But I get an error Association name expected, 'people' is not an association

What must I do to resolve this?

MaximePinot commented 4 years ago

Hi,

There is no attribute named people in your Petition entity.

Replace ['field' => 'people.lastname'] by ['field' => 'autor.lastname'].

By the way, autoris misspelled. It should be author (with a h).

evskorobogatij commented 4 years ago

Thank

And what must I do to output concatenated string like: ->add('author',TextColumn::class,["field" => "author.fam+author.im"]) ?

People entity have __toString() method, can I output his result into datatable column?

MaximePinot commented 4 years ago

You can use the render option to have full control over the output.

->add('author', TextColumn::class, [
    'field' => 'author.lastname',
    'render' => static function(string $value, Petition $petition): string {
        return $petition->getAuthor()->getFirstName() . ' ' . $petition->getAuthor()->getLastName();
        // return (string) $petition->getAuthor()
    }
])

See the Columns section in the documentation.

evskorobogatij commented 4 years ago

Thank you very much