Working with objects
$user = new User();
$user->first_name = "Ben";
$user->last_name = "Copsey";
$user->email = "ben@allseeing-i.com";
if (!$user->save()) {
print_r($user->validation_errors);
}
$user->agreed_to_terms_and_conditions = true;
$user->save();
$user = User::find_by_email("ben@allseeing-i.com");
print_r($user);
See the next section on finding objects for details of the find api.
Dependent objects
$user = new User();
$location_1 = new Location();
$location_1->name = "London";
$location_2 = new Location();
$location_2->name = "Paris";
$photo_1 = new Photo();
$photo_1->location = $location_1;
$photo_2 = new Photo();
$photo_2->location = $location_2;
$user->$photos = array($photo_1,$photo_2);
$user->save(); // Will save all the objects above
In the above example, when $user->save() is called, the following things will happen:
- The $user object will be saved
- The $user object will save each of the photo objects
- Each photo object will save its location
The values of foreign keys are set just before an object is written, so when a foreign key refers to an auto-increment key in another table, it will be set just before that object is written.