Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning: Undefined array key "user_id" in vendor/lm-commons/lmc-user/src/Mapper/UserHydrator.php on line 77 #49

Open
vrkansagara opened this issue Feb 21, 2024 · 12 comments

Comments

@vrkansagara
Copy link

vrkansagara commented Feb 21, 2024

I am using LmcUser + LmcRbacMvc with DoctrineOrmModule with MySQL

Composer.json

    "lm-commons/lmc-user": "^3.6",
    "lm-commons/lmc-admin": "^1.1",
    "doctrine/migrations": "^3.7",
    "lm-commons/lmc-rbac-mvc": "^3.3",
    "laminas/laminas-authentication": "^2.16",
    "doctrine/doctrine-orm-module": "^6.1",
    "doctrine/dbal": "^3.8"

image

It has following settings

  'user_entity_class' => \LmcUser\Entity\User::class,
@visto9259
Copy link
Member

@vrkansagara, this looks like you user table in your database does not have a user_id column.

What is the schema for the user table in your database?

The /data folder provides example schemas to build the user table.

@vrkansagara
Copy link
Author

vrkansagara commented Feb 21, 2024

@visto9259 I am trying to use LmcUser + LmcRbacMvc where I am getting this error. I have already configured using this way 'user_entity_class' => \User\Entity\User::class,

<?php

namespace User\Entity;

use LmcUser\Entity\User as LmcUserEntity;
use LmcRbacMvc\Identity\IdentityInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use LmcUser\Entity\UserInterface;
use Rbac\Role\RoleInterface;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User extends LmcUserEntity implements IdentityInterface
{

  /**
   * @var int|null
   *
   * @ORM\Id
   * @ORM\Column(name="user_id", type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;

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

  /**
   * @var string
   * @ORM\Column(name="email",type="string", length=128, unique=true, nullable=false)
   */
  protected $email;

  /**
   * @var string
   * @ORM\Column(name="display_name",type="string", length=128, unique=false, nullable=true)
   */
  protected $displayName;

  /**
   * @var string
   * @ORM\Column(name="password",type="string", length=128, unique=false, nullable=false)
   */
  protected $password;

  /**
   * @var int
   * @ORM\Column(name="state",type="integer", unique=false, nullable=true)
   */
  protected $state;

  /**
   * Get id.
   *
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * Set id.
   *
   * @param  int $id
   * @return UserInterface
   */
  public function setId($id)
  {
    $this->id = (int) $id;
    return $this;
  }

  /**
   * Get username.
   *
   * @return string
   */
  public function getUsername()
  {
    return $this->username;
  }

  /**
   * Set username.
   *
   * @param  string $username
   * @return UserInterface
   */
  public function setUsername($username)
  {
    $this->username = $username;
    return $this;
  }

  /**
   * Get email.
   *
   * @return string
   */
  public function getEmail()
  {
    return $this->email;
  }

  /**
   * Set email.
   *
   * @param  string $email
   * @return UserInterface
   */
  public function setEmail($email)
  {
    $this->email = $email;
    return $this;
  }

  /**
   * Get displayName.
   *
   * @return string
   */
  public function getDisplayName()
  {
    return $this->displayName;
  }

  /**
   * Set displayName.
   *
   * @param  string $displayName
   * @return UserInterface
   */
  public function setDisplayName($displayName)
  {
    $this->displayName = $displayName;
    return $this;
  }

  /**
   * Get password.
   *
   * @return string
   */
  public function getPassword()
  {
    return $this->password;
  }

  /**
   * Set password.
   *
   * @param  string $password
   * @return UserInterface
   */
  public function setPassword($password)
  {
    $this->password = $password;
    return $this;
  }

  /**
   * Get state.
   *
   * @return int
   */
  public function getState()
  {
    return $this->state;
  }

  /**
   * Set state.
   *
   * @param  int $state
   * @return UserInterface
   */
  public function setState($state)
  {
    $this->state = $state;
    return $this;
  }

  /**
   * @var Collection
   * @ORM\ManyToMany(targetEntity="\User\Entity\FlatRole")
   * @ORM\JoinTable(name="user_flatrole",
   *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
   * )
   */
  private $roles;

  public function __construct()
  {
    $this->roles = new ArrayCollection();
  }

  /**
   * {@inheritDoc}
   */
  public function getRoles()
  {
    return $this->roles->toArray();
  }

  /**
   * Set the list of roles
   * @param Collection $roles
   */
  public function setRoles(Collection $roles)
  {
    $this->roles->clear();
    foreach ($roles as $role) {
      $this->roles[] = $role;
    }
  }

  /**
   * Add one role to roles list
   * @param \Rbac\Role\RoleInterface $role
   */
  public function addRole(RoleInterface $role)
  {
    $this->roles[] = $role;
  }

}

User table structure look like this.

image

@vrkansagara
Copy link
Author

I also running with the another issue when I switched to the above User.php entity as bellow
image

@visto9259
Copy link
Member

For the roles issue, do you have a roles column in your table?
I don't see it the screen capture of the table structure that you shared above.

@vrkansagara
Copy link
Author

That roles come from the relation , I think @visto9259

@vrkansagara
Copy link
Author

vrkansagara commented Feb 21, 2024

Problems looks like the guest role is not assigned while user creation is fired.
image

This role should be assigned as default while user is being created using LmcRbacMvc

I do also suspect that user_id is not mapped while user is being registered.

@vrkansagara
Copy link
Author

image

This patch should be needed here

    public function hydrate(array $data, $object)
    {
        if (!$object instanceof UserEntityInterface) {
            throw new Exception\InvalidArgumentException('$object must be an instance of LmcUser\Entity\UserInterface');
        }

        if (isset($data['id'])) {
          $data = $this->mapField('user_id', 'id', $data);
        }

        return $this->hydrator->hydrate($data, $object);
    }

looks like there is issue here https://github.com/LM-Commons/LmcUser/blob/master/src/Mapper/UserHydrator.php#L64

@visto9259
Copy link
Member

Hi @vrkansagara ,
I am still trying to reproduce the issues in my own dev setup.
Have you found a resolution or do you have more info to provide?
Are you using ORM for the user entities? Off the shelf, LmcUser does not use ORM for the user table. There is another module to use ORM for user https://github.com/LM-Commons/LmcUserDoctrineORM.
LmcUser when fetching users does not use ORM (I think) and since the roles come from the relation defined in your entity, roles do not get pulled into the user entity.

@vrkansagara
Copy link
Author

yes, @visto9259 , I do confirm that I was not using LmcUserDoctrineORM. so facing this issue.

I also facing much difficult to setup the FlatRole using LmcUserMVC so can you consider to as proposal for the demo repository where it can show the working model for following packages.

  • LmcUser
  • LmcAdmin
  • LmcRbacMvc
  • LmcUserDoctrineORM

this combination is useful to show the basic Dashboard application so any one can easily start using this combination package and start creating application.

@visto9259
Copy link
Member

That would be a good idea but we lack the bandwidth to do so. This is all based on volunteer work.
I wrote a tutorial on the basics of how to use LmcUser: https://www.vistomedia.com/laminas-user-authentication-using-lmcuser/
Take a look. I intended to write a tutorial on LmcRbacMvc as a follow up but did not have the time to get to it.

There are thousands of installs of LmcUser and LmcRbacMvc without issues. There could be bugs in the code but I suspect that there is something not set up properly in your application.

Can you share details on how you define the database adapter in your config? LmcUser uses the class \Laminas\Db\Adapter\Adapter::class as the database adapter. You need a database config somewhere in your config files which I assume you have.
Please share your lmc_user and lmc_rbac configs.

@vrkansagara
Copy link
Author

@visto9259 , I figured out the issue as i said here. #49 (comment)

So this issue can be closed.

I have couple of improvement , I will make it PR

@visto9259
Copy link
Member

This is just the explanation for the warning and that I knew that the issue was where you pointed out.
You can submit a PR for it.
What about the other issues that you reported?

@visto9259 visto9259 reopened this Feb 27, 2024
This was referenced Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

No branches or pull requests

2 participants