Skip to content

Commit

Permalink
change in index.php
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulRawat1994 committed Oct 2, 2018
1 parent 0a47d94 commit 3756ad5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
8 changes: 5 additions & 3 deletions Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ abstract class AbstractEntity
* Class constructor
* @param Array
*/
public function __construct(array $data)
public function __construct(array $data = null)
{
foreach ($data as $name => $value) {
$this->$name = $value;
if (!is_null($data)) {
foreach ($data as $name => $value) {
$this->$name = $value;
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class User extends AbstractEntity
*/
public function __construct(array $data = null)
{
if (!is_null($data)) {
parent::__construct($data);
}
parent::__construct($data);
}
/**
* Set the user's ID
Expand Down
2 changes: 1 addition & 1 deletion Mapper/AbstractDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function _setEntityClass($entityClass)
*/
public function getEntityClass()
{
return $this->_entityClass;
return new $this->_entityClass;
}

/**
Expand Down
26 changes: 22 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
ini_set('display_errors', 1);

use Database\PdoAdapter;
use Mapper\UserMapper;
use Entity\User;
use Mapper\UserMapper as User;

// Bootstrap all classes
require_once 'Autoloader.php';
Expand All @@ -19,11 +18,30 @@
// PATH CONSTANT
define('ENTITY_PATH', '\Entity\\');

//Working
// Create Database connection
$db = new PdoAdapter($conn_string, $username, $password);
$db->openConnection();

$userMapper = new UserMapper($db);
$userMapper = new User($db);

// find all records
$user=$userMapper->findAll();
print_r($user[0]->lname);

// Insert New Record
$user= $userMapper->getEntityClass();
$user->fname = 'Sam';
$user->lname = 'William';
$user->email = 'samw@gmail.com';
$userMapper->insert($user);

// Update existing document
$user= $userMapper->getEntityClass();
$user->id = 5;
$user->fname = 'Pankaj';
$userMapper->update($user);

// Delete existing document
$user= $userMapper->getEntityClass();
$user->id = 5;
$userMapper->delete($user);

0 comments on commit 3756ad5

Please sign in to comment.