xml version="1.0" encoding="UTF-8" ?>
Now that we have rows in our user table (assuming you're already visited the Value Objects) page, we'll demonstrate the “Selector” classes.
We'll start out by simply selecting a user by their primary key (`user_id`).
$selector = new UserSelector(2);
echo "$selector<br/>";
$resultCollection = $selector->fetch();
echo "$resultCollection<br/>";
UserSelector current "where" clause: “ WHERE `user`.`user_id` = 2”
AbstractSelector::fetch() using query: “SELECT * FROM `user` WHERE `user`.`user_id` = 2”
AbstractCollection::__toString()
Raw VO Data (Total = 1):
Raw 0: Array
(
[user_id] => 2
[username] => Method3User
[pwd] => Method3Pwd
)
Built UserVO Objects (Total = 0 of 1):
The Selector classes all accept the table's primary keys in their constructor. This way, if you want to select a specific row by key you can prep the instance right away.
Note that even though only one row is found, the selector returns a collection. This allows you to code without testing if the result is a Collection or a single Value Object.
Now let's make a selection that will return multiple rows and demonstrate some of the functionality of the selector. Every selector object has a column selector for each column in the table and other query crafting methods.
$selector->select()->userId(">", 2)->orderBy('user_id',"DESC")->limit(0,11);
$resultCollection = $selector->fetch();
echo $resultCollection;
AbstractSelector::fetch() using query: “SELECT * FROM `user` WHERE `user`.`user_id` > 2 ORDER BY user_id DESC LIMIT 0, 11 ”
AbstractCollection::__toString()
Raw VO Data (Total = 6):
Raw 0: Array
(
[user_id] => 8
[username] => Anaximenes_113
[pwd] => c9f0f_8
)
Raw 1: Array
(
[user_id] => 7
[username] => YourName_135
[pwd] => 8f14_7
)
Raw 2: Array
(
[user_id] => 6
[username] => Bob_260
[pwd] => 16_6
)
Raw 3: Array
(
[user_id] => 5
[username] => YourName_135
[pwd] => e4d_5
)
Raw 4: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Raw 5: Array
(
[user_id] => 3
[username] => Therese_25
[pwd] => eccb_3
)
Built UserVO Objects (Total = 0 of 6):
The order of the *non* column selector method calls (e.g. "orderBy(), limit(), etc.)" is independent of the query constructed , unlike the column specific methods, so you can modify the query as you see fit. We'll build a few more queries to demonstrate. (Also note that the select() method is optional and it resets the query to empty.)
$selector->select()->orderBy("`user_id`", "ASC")->limit(0, 15)->username("LIKE", "%x%")->orWith()->pwd("LIKE", "%7%")->andWith()->userId(">", 3)->andWith()->userId("<", 19);
$resultCollection = $selector->fetch();
echo $resultCollection;
AbstractSelector::fetch() using query: “SELECT * FROM `user` WHERE `user`.`username` LIKE '%x%' OR `user`.`pwd` LIKE '%7%' AND `user`.`user_id` > 3 AND `user`.`user_id` < 19 ORDER BY `user_id` ASC LIMIT 0, 15 ”
AbstractCollection::__toString()
Raw VO Data (Total = 3):
Raw 0: Array
(
[user_id] => 4
[username] => MyName_234
[pwd] => a87_4
)
Raw 1: Array
(
[user_id] => 7
[username] => YourName_135
[pwd] => 8f14_7
)
Raw 2: Array
(
[user_id] => 8
[username] => Anaximenes_113
[pwd] => c9f0f_8
)
Built UserVO Objects (Total = 0 of 3):