Skip to content

Commit

Permalink
Merge pull request #591 from stijnhau/user_id-to-id
Browse files Browse the repository at this point in the history
User id to id
  • Loading branch information
Danielss89 authored Jul 8, 2016
2 parents 245c20b + dd9280f commit 30a38e4
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion data/schema.ibmdb2.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE user
(
user_id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
username VARCHAR(255) DEFAULT NULL UNIQUE,
email VARCHAR(255) DEFAULT NULL UNIQUE,
display_name VARCHAR(50) DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion data/schema.mysql.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE `user`
(
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(255) DEFAULT NULL UNIQUE,
`email` VARCHAR(255) DEFAULT NULL UNIQUE,
`display_name` VARCHAR(50) DEFAULT NULL,
Expand Down
4 changes: 2 additions & 2 deletions data/schema.pgsql.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
CREATE TABLE public.user
(
user_id serial NOT NULL,
id serial NOT NULL,
username character varying(255) DEFAULT NULL UNIQUE,
email character varying(255) DEFAULT NULL UNIQUE,
display_name character varying(50) DEFAULT NULL,
password character varying(128) NOT NULL,
state smallint,

CONSTRAINT user_pkey PRIMARY KEY (user_id),
CONSTRAINT user_pkey PRIMARY KEY (id),
CONSTRAINT user_username_key UNIQUE (username),
CONSTRAINT user_email_key UNIQUE (email)
);
2 changes: 1 addition & 1 deletion data/schema.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE user
(
user_id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
username VARCHAR(255) DEFAULT NULL UNIQUE,
email VARCHAR(255) DEFAULT NULL UNIQUE,
display_name VARCHAR(50) DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion data/schema.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE user
(
user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username VARCHAR(255) DEFAULT NULL UNIQUE,
email VARCHAR(255) DEFAULT NULL UNIQUE,
display_name VARCHAR(50) DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion docs/check-user-logged-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ZfcUser provides a Controller Plugin ([zfcUserAuthentication](https://github.com
if ($this->zfcUserAuthentication()->hasIdentity()) {
//get the email of the user
echo $this->zfcUserAuthentication()->getIdentity()->getEmail();
//get the user_id of the user
//get the id of the user
echo $this->zfcUserAuthentication()->getIdentity()->getId();
//get the username of the user
echo $this->zfcUserAuthentication()->getIdentity()->getUsername();
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-action-when-user-register.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $em->attach('ZfcUser\Service\User', 'register', function($e) {
```

## Retrieving the User Id
If you need to retrieve the `user_id`, just attach to `register.post` and the user entity should have it.
If you need to retrieve the `id`, just attach to `register.post` and the user entity should have it.

## Example

Expand Down
2 changes: 1 addition & 1 deletion src/ZfcUser/Form/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct()
));

$this->add(array(
'name' => 'userId',
'name' => 'id',
'type' => 'Zend\Form\Element\Hidden',
'attributes' => array(
'type' => 'hidden'
Expand Down
2 changes: 1 addition & 1 deletion src/ZfcUser/Form/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($name, RegistrationOptionsInterface $options)
$this->setRegistrationOptions($options);
parent::__construct($name);

$this->remove('userId');
$this->remove('id');
if (!$this->getRegistrationOptions()->getEnableUsername()) {
$this->remove('username');
}
Expand Down
6 changes: 3 additions & 3 deletions src/ZfcUser/Mapper/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function findByUsername($username)
public function findById($id)
{
$select = $this->getSelect()
->where(array('user_id' => $id));
->where(array('id' => $id));

$entity = $this->select($select)->current();
$this->getEventManager()->trigger('find', $this, array('entity' => $entity));
Expand All @@ -53,14 +53,14 @@ public function insert($entity, $tableName = null, Hydrator $hydrator = null)
{
$hydrator = $hydrator ?: $this->getHydrator();
$result = parent::insert($entity, $tableName, $hydrator);
$hydrator->hydrate(array('user_id' => $result->getGeneratedValue()), $entity);
$entity->setId($result->getGeneratedValue());
return $result;
}

public function update($entity, $where = null, $tableName = null, Hydrator $hydrator = null)
{
if (!$where) {
$where = array('user_id' => $entity->getId());
$where = array('id' => $entity->getId());
}

return parent::update($entity, $where, $tableName, $hydrator);
Expand Down
6 changes: 4 additions & 2 deletions src/ZfcUser/Mapper/UserHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public function extract($object)
{
$this->guardUserObject($object);
$data = parent::extract($object);
return $this->mapField('id', 'user_id', $data);
if ($data['id'] === null) {
unset($data['id']);
}
return $data;
}

/**
Expand All @@ -50,7 +53,6 @@ public function extract($object)
public function hydrate(array $data, $object)
{
$this->guardUserObject($object);
$data = $this->mapField('user_id', 'id', $data);
return parent::hydrate($data, $object);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ZfcUserTest/Form/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function testConstruct()
$this->assertArrayHasKey('password', $elements);
$this->assertArrayHasKey('passwordVerify', $elements);
$this->assertArrayHasKey('submit', $elements);
$this->assertArrayHasKey('userId', $elements);
$this->assertArrayHasKey('id', $elements);
}
}
4 changes: 2 additions & 2 deletions tests/ZfcUserTest/Form/RegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testConstruct()

$elements = $form->getElements();

$this->assertArrayNotHasKey('userId', $elements);
$this->assertArrayNotHasKey('id', $elements);
$this->assertArrayNotHasKey('username', $elements);
$this->assertArrayNotHasKey('display_name', $elements);
$this->assertArrayHasKey('email', $elements);
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testSetGetRegistrationOptions()
* @param mixed $value = null
* @return \ReflectionProperty
*/
public function helperMakePropertyAccessable ($objectOrClass, $property, $value = null)
public function helperMakePropertyAccessable($objectOrClass, $property, $value = null)
{
$reflectionProperty = new \ReflectionProperty($objectOrClass, $property);
$reflectionProperty->setAccessible(true);
Expand Down
9 changes: 3 additions & 6 deletions tests/ZfcUserTest/Mapper/UserHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testHydrateWithValidUserObject()
'display_name' => 'ZfcUser',
'password' => 'c4zyP455w0rd!',
'state' => '1',
'user_id' => 1
'id' => 1
);

$result = $this->hydrator->hydrate($expectArray, $user);
Expand All @@ -84,7 +84,7 @@ public function testHydrateWithValidUserObject()
$this->assertEquals($expectArray['display_name'], $result->getDisplayName());
$this->assertEquals(static::ENCRYPTED_PASSWORD, $result->getPassword());
$this->assertEquals((int) $expectArray['state'], $result->getState());
$this->assertEquals($expectArray['user_id'], $result->getId());
$this->assertEquals($expectArray['id'], $result->getId());
}

public function provideValidUserObjects()
Expand All @@ -98,7 +98,7 @@ public function provideValidUserObjects()
'display_name' => 'ZfcUser',
'password' => 'ZfcUserPassword',
'state' => 1,
'user_id' => 1
'id' => 1
);

$return[]=array($this->buildUser($buffer), $buffer);
Expand All @@ -123,9 +123,6 @@ private function buildUser(array $data)
{
$user = new UserEntity;
foreach ($data as $key => $value) {
if ($key == 'user_id') {
$key = 'id';
}
$method = 'set' . str_replace(" ", "", ucwords(str_replace("_", " ", $key)));
call_user_func(array($user, $method), $value);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ZfcUserTest/Mapper/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public function providerTestFindBy()
array($user->getId()),
array(
'whereArgs'=>array(
array('user_id'=>$user->getId()),
array('id'=>$user->getId()),
'AND'
)
),
Expand Down

0 comments on commit 30a38e4

Please sign in to comment.