0

I have An entity Product Manged By EasyAdmin, i ALSO USE fos UserbUNDLE for user Management. Everything works fine but when i add a new user in easyadmin i have a dropdown of user but i want to have automatically the authenticated user.

I have defined an one to Many relation between User and Product in an attribut of the class Product.

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

Product Entity:

<?php

namespace AppBundle\Entity;

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


/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
* @Vich\Uploadable
*/
class Product
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

 /**
 * @ORM\ManyToOne(targetEntity="Type")
 * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
 */
private $type;

/**
 * @ORM\Column(type="boolean")
 */
public $status;

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

User :

<?php


namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

public function __construct()
{
    parent::__construct();
    // your own logic
}
}

How to get it automatically?

mokas
  • 13
  • 8

2 Answers2

0

Can we have more details ? Both entities and controller because i think your not using the good targetEntity

A beginner
  • 124
  • 1
  • 14
  • thanks @luis-rosario. Please see update question. the persistence of entity is managed directly by easy admin. – mokas Jul 25 '18 at 15:39
0

i had quite the same issue,i mean,in some cases i was needing to get ONLY THE CURRENT LOGGED USER INFO INSIDE MY SELECT, and sometimes i was needing to get the full list of users from the database

maybe the solution i found could help you:

Get the logged user info in the easyadmin select

to make it simple you have to override the easyadmin formbuilder and ask only the currently logged user info

$formBuilder->add('user', EntityType::class, array(
                'class' => 'AppBundle:User',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                    ->where('u.id = :id')->setParameter('id',$this->getUser()->getId());
                },
            ));
            return $formBuilder;
ultime
  • 11
  • 3