1

I have 3 entities : User , UserRole , UserProfile. My primary key for the userprofile entity is actually a foreign key from user entity which is the UserId. Everytime I try to access userProfile using the foreign key userid. I always got an error of undefined index of userID.

this is the exact error message

**> Notice: Undefined index: userID in

C:\wamp\www\SymfonyFolder\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 2583**

Controller ..... ....

public function profileAction($id)
    {
        $user = new User();
        $user_repository = $this->getDoctrine()->getRepository('AcmePodUserBundle:User');
        $user = $user_repository->find($id);

        $userprofile = new UserProfile();
        $userprofile_repository = $this->getDoctrine()->getRepository('AcmeUserBundle:UserProfile');       
        $userprofile = $userprofile_repository->find($user);

       return new Response($userprofile); // this is not the actual output i need. I just used this simply to test if userprofile has some value. 
}

User.orm.yml

type:  entity
  table: reg_user
  oneToOne:
  id:
    userID:
      column: user_id
      type: integer
      generator: { strategy: AUTO }
      targetEntity: UserProfile
      mappedBy: userProfileID

UserProfile.orm.yml

type:  entity
  table: users_profile
  id:
    userProfileID:
      associationKey: true
  fields:
    firstName:
      column: first_name
      type: string
      length: 255
      unique: true
      nullable: false
    lastName:
      column: last_name
      type: string
      length: 255
      nullable: false
 oneToOne:
    userProfileID:
      targetEntity: User
      inversedBy: userID
      joinColumn:
        name: fk_user_id
        referencedColumnName: user_id

User.php

class User implements AdvancedUserInterface, \Serializable
{

// @var auto integer
protected $userID;

// @var string
protected $userName;

// @var string
protected $salt;

// @var string - must not persist to DB
protected $plainPassword;

// @var string
protected $password;

// @var string
protected $email;

// @var boolean
protected $isActive;

// @var string
protected $confirmationCode;

// @var datetime
protected $createdAt;

// @var string - must not persist to DB
protected $chooseRole;

// @var for roles fk_role_id - mapped
protected $userRole;


/* _construct() is an instant instance of the entity*/

public function __construct()
{
    date_default_timezone_set('Asia/Manila');
    $this->createdAt = new \DateTime("now");

//all users are inactive upon registration.
//isActive turns 'true' with the use of email confirmation
    $this->isActive = false; 
}

public function __toString()
{
    return strval($this->userID);
}

/**
 * Get userID
 *
 * @return integer 
 */
public function getUserID()
{
    return $this->userID;
}

/**
 * Set userName
 *
 * @param string $userName
 * @return User
 */
public function setUserName($userName)
{
    $this->userName = $userName;

    return $this;
}

/**
 * Get userName
 *
 * @return string 
 */
public function getUserName()
{
    return $this->userName;
}

/**
 * Set salt
 *
 * @param string $salt
 * @return User
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Get salt
 *
 * @return string 
 */
public function getSalt()
{
    return $this->salt;
}


public function setPlainPassword($plainPassword)
{
    $this->plainPassword = $plainPassword;

    return $this;
}

public function getPlainPassword()
{
    return $this->plainPassword;
}

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

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set isActive
 *
 * @param boolean $isActive
 * @return User
 */
public function setIsActive($isActive)
{
    $this->isActive = $isActive;

    return $this;
}

/**
 * Get isActive
 *
 * @return boolean 
 */
public function getIsActive()
{
    return $this->isActive;
}

/**
 * Set confirmationCode
 *
 * @param string $confirmationCode
 * @return User
 */
public function setConfirmationCode($confirmationCode)
{
    $this->confirmationCode = $confirmationCode;

    return $this;
}

/**
 * Get confirmationCode
 *
 * @return string 
 */
public function getConfirmationCode()
{
    return $this->confirmationCode;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return User
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime 
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

public function setChooseRole($chooseRole)
{
    $this->chooseRole = $chooseRole;

    return $this;
}

public function getChooseRole()
{
    return $this->chooseRole;
}

/**
 * Set userRole
 *
 * @param CloudPod\UserBundle\Entity\UserRoles $userRole
 * @return User
 */
public function setUserRole(\CloudPod\UserBundle\Entity\UserRoles $userRole = null)
{
    $this->userRole = $userRole;

    return $this;
}

/**
 * Get userRole
 *
 * @return CloudPod\UserBundle\Entity\UserRoles 
 */
public function getUserRole()
{
    return $this->userRole;
}


/**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    return serialize(array(
        $this->userID,
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list (
        $this->userID,
    ) = unserialize($serialized);
}

public function isAccountNonExpired()
{ return true; }

public function isAccountNonLocked()
{ return true; }

public function isCredentialsNonExpired()
{ return true; }

public function isEnabled()
{ return $this->isActive; }

   /**
 * @inheritDoc
 */
public function eraseCredentials()
{
}

public function getRoles()
{
  return array($this->userRole);  
}

}

I think the problem is on my controller codes. Can somebody tell me the proper way of coding it. P.S I seperated the user to user profile because i dont want to mix up in a table the user account data(like username, role, etc.) with its user info.

Teffi
  • 2,498
  • 4
  • 21
  • 40
  • what does the User Entity look like? – Adam Elsodaney Dec 21 '12 at 18:25
  • Try `findByUserId($id)` since you specified a column in your orm. – phpisuber01 Dec 21 '12 at 18:49
  • So is something not working? It is just a notice after all... Maybe the ORM Class in UnitOfWork.php does not check proberly for the array key... – madflow Dec 22 '12 at 17:40
  • @madflow The only problem is that I cant seem to get the column rows of the userProfile table using its primary key userID which is a foreign key base on userID on the User table. – Teffi Dec 23 '12 at 15:13

0 Answers0