简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM