Lasith
Lasith

Reputation: 565

sonata admin use two entities in an admin

im new to sonata admin, is this possible to use two entities in one admin class?

my User entity,

App\MyBundle\Entity\Users.php

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

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=45, nullable=true)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=100, nullable=true)
 */
private $email;

my UserProject entity,

App\MyBundle\Entity\UserProjects.php

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

/**
 * @var \User
 *
 * @ORM\ManyToOne(targetEntity="Users")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="userId", referencedColumnName="id")
 * })
 */
private $userid;

/**
 * @var array
 *
 * @ORM\Column(name="projectId", type="array")
 */
private $projects;

my Admin class,

class UserAdmin extends SonataUserAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
         $formMapper
        ->with('General') // these fields from Users Entity
            ->add('username')
            ->add('email')

         ->with('Projects') // these fields from UserPrjects Entity

         /* here i need to add a field for projects related to current user */
      }
  }

is there any way to get these two entities connect together?

Upvotes: 0

Views: 1336

Answers (1)

rpg600
rpg600

Reputation: 2850

I suggest you to add a One-To-Many in the User side:

/**
 * @ORM\OneToMany(targetEntity="UserProjects", mappedBy="userid")
 */
protected $userProjects;

The you can use the UserProjects entity.

Upvotes: 1

Related Questions