How do I add/update/delete records?

From PHP on Trax

Jump to: navigation, search

Contents

Example 1

There is built in functionality for inserting, updating, and deleting records in ActiveRecord. Below is an example of how to and insert, update, and delete from a form in Trax. So lets say we have a Table called users the below would be a controller for adding, updating and deleting users.

Users Manager Controller (app/controllers/users_manager_controller.php)



<?
class UsersManager extends ApplicationController {
    function 
index() {
        
$user = new User();
        
$this->users $user->find_all();
    }
    
    function 
add() {
        
# you can pass the array of key => value params to the contructor
        # or into the save function. as well you can set them directly if 
        # wanted to such as $this->user->first_name = "John";
        
$this->user = new User($_POST['user']);
        if(
$_POST) {
            if(
$this->user->save()) { 
                
Session::flash('notice''User added succesfully');
                
$this->redirect_to url_for(array(":action" => "index"));            
            }   
        }
    }
    
    function 
edit() {
        
$user = new User();
        
$this->user $user->find($_REQUEST['id']);
        if(
$_POST) {
            if(
$this->user->save($_POST['user'])) { 
                
Session::flash('notice''User updated succesfully');
                
$this->redirect_to url_for(array(":action" => "index"));            
            }   
        }   
    }
    
    function 
delete() {
        
$user = new User();
        
$user->load($_REQUEST['id']);
        if(
$user->delete()) {
            
Session::flash('notice''User updated succesfully');                          
        }    
        
$this->redirect_to url_for(array(":action" => "index")); 
    }
}
?>

User Model (app/models/user.php)



<?
class User extends ActiveRecord {
    public 
$has_many "orders";  
}
?>

Example 2

Now lets say you have a shopping cart and you are placing an order and you have a table called products, orders, and order_items.



<?
$order 
= new Order($_REQUEST['order']);
foreach(
$_REQUEST['products_to_buy'] as $prod_id => $qty) {
    
$product = new Product();
    
$product $product->find($prod_id);
    
# or you can do this instead $product->load($prod_id); 
    
$order_items[] = new OrderItem(array("product" => $product,
                                         
"quantity" => $qty)); 
}
$order->order_items $order_items;
$order->save();
?>

$_REQUEST['order'] is an array from the form with generic stuff about the order to go into the order table. $_REQUEST['products_to_buy'] is an array from the form of the products that are to bought for this order.

OrderItem Model (app/models/order_item.php)



<?
class OrderItem extends ActiveRecord {
    public 
$belongs_to "order,product";      
}
?>


Order Model (app/models/order.php)



<?
class Order extends ActiveRecord {    
    public 
$has_many "order_items"
    public 
$belongs_to "user"
}
?>
Personal tools