xml version="1.0" encoding="UTF-8" ?>
The VO Table Maps are utility classes used by the Selectors, Collections, and VOs. They provide convenience methods for building Collections and concrete VOs.
You can use the table maps to retrieve a table row's values in the correct ValueObject type.
$userMap = new UserDbMap();
$userId = 3;
$voKeyAry = $userMap->getKeyAry($userId);//getKeyAry() returns an associative array built specifically for the type of VO
$user3 = $userMap->findByKeyNow($voKeyAry);//findByKeyNow() accepts a single associative array “key” that identifies a table row
$userId = 4;
$voKeyAry = $userMap->getKeyAry($userId)
$user4 = $userMap->findByKeyNow($voKeyAry);
echo "$user3<br/>";
echo "$user4<br/>";
DbExchange::has() Looking for 'UserVOArray'
UserDbMap::findByKeyNow() using: Array
(
[user_id] => 3
)
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array
(
[user_id] => 3
[username] => Therese_161
[pwd] => eccb_3
)
DbExchange::has() Looking for 'UserVOArray'
UserDbMap::findByKeyNow() using: Array
(
[user_id] => 4
)
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
(UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
(UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
Since each concrete DbMap knows the table's primary key, it has methods for accessing the primary key. These methods are used to keep track of and compare VO instances.
echo "VO table key string: “{$userMap->getKeyStr($user3)}”<br/>";
echo "VO global key string: “{$userMap->globalKeyStr($user3)}” (used to distinguish VOs in a script where different VOs may have the same P.K.)<br/>";
VO table key string: “3”
VO global key string: “UserVO.3” (used to distinguish VOs in a script where different VOs may have the same P.K.)
Each table map also contains a method to get an empty VO collection instance. You can then add VOs to the collection individually.
$userCollection = $userMap->getCollection();
echo $userCollection;
$userCollection->push($user3);
$userCollection->push($user4);
echo $userCollection;
AbstractCollection::__toString()
Raw VO Data (Total = 0):
Built UserVO Objects (Total = 0 of 0):
AbstractCollection::push() adding UserVO.3 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” } to an associative array...
UserDbMap::voToArray() converting UserVO.3 back to an associative array
AbstractCollection::push() adding UserVO.4 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” } to an associative array...
UserDbMap::voToArray() converting UserVO.4 back to an associative array
AbstractCollection::__toString()
Raw VO Data (Total = 2):
Raw 0: Array
(
[user_id] => 3
[username] => Therese_161
[pwd] => eccb_3
)
Raw 1: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Built UserVO Objects (Total = 2 of 2):
VO 0: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
VO 1: (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
First we'll do it using our already built user objects.
$rawAry = array();//An empty array of raw user data
$voAry = array($user3, $user4);//An array of already build VOs
$userCollection = $userMap->getCollection($rawAry, $voAry);
echo $userCollection;
AbstractCollection::push() adding UserVO.3 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” } to an associative array...
UserDbMap::voToArray() converting UserVO.3 back to an associative array
AbstractCollection::push() adding UserVO.4 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” } to an associative array...
UserDbMap::voToArray() converting UserVO.4 back to an associative array
AbstractCollection::__toString()
Raw VO Data (Total = 2):
Raw 0: Array
(
[user_id] => 3
[username] => Therese_161
[pwd] => eccb_3
)
Raw 1: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Built UserVO Objects (Total = 2 of 2):
VO 0: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
VO 1: (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
Then we'll make a “custom” query to the database just so we can get the raw result data back and use it to demonstrate how the Selector classes build the collection after it gets a result.
Note that the $voAry is an *optional* array of already built VOs you want to include in the collection object.
Note also that the getCollection method deconstructs the pre-built VOs and adds them to the raw array.
$rawAry = array();//An empty array of raw user data
$voAry = array($user3, $user4);//An *optional* array of already built VOs
$sql = "SELECT * FROM `user` ORDER BY `user_id` DESC LIMIT 5";
/* @var $result mysqli_result */
$result = DbTest::Mysqli()->query($sql);
while( $rawRow = $result->fetch_assoc()){
$rawAry[] = $rawRow;
}
$userCollection = $userMap->getCollection($rawAry, $voAry);
echo $userCollection;
AbstractCollection::push() adding UserVO.3 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” } to an associative array...
UserDbMap::voToArray() converting UserVO.3 back to an associative array
AbstractCollection::push() adding UserVO.4 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” } to an associative array...
UserDbMap::voToArray() converting UserVO.4 back to an associative array
AbstractCollection::__toString()
Raw VO Data (Total = 7):
Raw 0: Array
(
[user_id] => 41928
[username] => Username8
[pwd] => c9f0f89
)
Raw 1: Array
(
[user_id] => 41926
[username] => Username6
[pwd] => 1679091
)
Raw 2: Array
(
[user_id] => 41924
[username] => Username4
[pwd] => a87ff67
)
Raw 3: Array
(
[user_id] => 41923
[username] => Username3
[pwd] => eccbc87
)
Raw 4: Array
(
[user_id] => 41922
[username] => Username2_120
[pwd] => c81e728
)
Raw 5: Array
(
[user_id] => 3
[username] => Therese_161
[pwd] => eccb_3
)
Raw 6: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Built UserVO Objects (Total = 2 of 7):
VO 5: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
VO 6: (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
There are several other methods in the table maps, but they aren't really intended to be used externally. There is no problem using the methods themselves, but using these methods explicitly circumvents their original purpose.
$allUsersCollection = $userMap->getAll();
echo $allUsersCollection;
AbstractCollection::__toString()
Raw VO Data (Total = 13):
Raw 0: Array
(
[user_id] => 2
[username] => Method3User
[pwd] => Method3Pwd
)
Raw 1: Array
(
[user_id] => 3
[username] => Therese_161
[pwd] => eccb_3
)
Raw 2: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Raw 3: Array
(
[user_id] => 5
[username] => YourName_135
[pwd] => e4d_5
)
Raw 4: Array
(
[user_id] => 6
[username] => Bob_260
[pwd] => 16_6
)
Raw 5: Array
(
[user_id] => 7
[username] => YourName_135
[pwd] => 8f14_7
)
Raw 6: Array
(
[user_id] => 8
[username] => Anaximenes_113
[pwd] => c9f0f_8
)
Raw 7: Array
(
[user_id] => 41928
[username] => Username8
[pwd] => c9f0f89
)
Raw 8: Array
(
[user_id] => 41926
[username] => Username6
[pwd] => 1679091
)
Raw 9: Array
(
[user_id] => 41924
[username] => Username4
[pwd] => a87ff67
)
Raw 10: Array
(
[user_id] => 41923
[username] => Username3
[pwd] => eccbc87
)
Raw 11: Array
(
[user_id] => 41922
[username] => Username2_120
[pwd] => c81e728
)
Raw 12: Array
(
[user_id] => 41918
[username] => theUsername
[pwd] => thePwd
)
Built UserVO Objects (Total = 0 of 13):
AbstractCollection::__toString()
Raw VO Data (Total = 13):
Raw 0: Array
(
[user_id] => 2
[username] => Method3User
[pwd] => Method3Pwd
)
Raw 1: Array
(
[user_id] => 3
[username] => Therese_161
[pwd] => eccb_3
)
Raw 2: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Raw 3: Array
(
[user_id] => 5
[username] => YourName_135
[pwd] => e4d_5
)
Raw 4: Array
(
[user_id] => 6
[username] => Bob_260
[pwd] => 16_6
)
Raw 5: Array
(
[user_id] => 7
[username] => YourName_135
[pwd] => 8f14_7
)
Raw 6: Array
(
[user_id] => 8
[username] => Anaximenes_113
[pwd] => c9f0f_8
)
Raw 7: Array
(
[user_id] => 41928
[username] => Username8
[pwd] => c9f0f89
)
Raw 8: Array
(
[user_id] => 41926
[username] => Username6
[pwd] => 1679091
)
Raw 9: Array
(
[user_id] => 41924
[username] => Username4
[pwd] => a87ff67
)
Raw 10: Array
(
[user_id] => 41923
[username] => Username3
[pwd] => eccbc87
)
Raw 11: Array
(
[user_id] => 41922
[username] => Username2_120
[pwd] => c81e728
)
Raw 12: Array
(
[user_id] => 41918
[username] => theUsername
[pwd] => thePwd
)
Built UserVO Objects (Total = 0 of 13):