PHP Classes

File: insert.php

Recommend this page to a friend!
  Classes of João Mário Nedeff   Model   insert.php   Download  
File: insert.php
Role: Example script
Content type: text/plain
Description: Example of an INSERT operation
Class: Model
Map objects to table records using Active Record
Author: By
Last change:
Date: 12 years ago
Size: 2,305 bytes
 

Contents

Class file image Download
<?php

/* Example Script INSERT operation
 *
 * Copyright 2012 Soluções Office (www.solucoesoffice.com.br)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
/* Example table name: user
 * Fields:
 * id INT Primary Key, must be present, although it doesn't need to be the first field
 * date_created DATETIME optional. If present, is automatically filled with the date and time of the INSERT operation.
 * date_updated DATETIME optional. If present, is automatically filled with the date and time of the INSERT/UPDATE operation.
 * name VARCHAR(100)
 * surname VARCHAR(100)
 * username VARCHAR(50)
 * password VARCHAR(50)
*/

/* Folder structure:
 * /project/Configs
 * core.ini
 * /project/library
 * Configs.php
 * DConnect.php
 * Model.php
 * /project/insert.php
*/

//Requires of the dependencies
require 'library/Configs.php';
require
'library/DConnect.php';
require
'library/Model.php';

//Defines the path to the Configs INI folder.
define(CONFIGS, 'Configs');
define('DS', DIRECTORY_SEPARATOR);

//The class USER extends Model, and has the same name as the table to which it refers
class User extends Model {}

//INSERT
$user = new User();
$user->name = 'Test User';
$user->surname = 'Surname';
$user->username = 'Username';
$user->password = 'Password';

//INSERTS the object. Returns the Row's ID, or PDOException object if error.
$id = $user->save();

//to retrieve id, one can use $Id, or $user->id
echo $user->id . ' ' . $user->name . ' ' . $user->surname . ' ' . $user->username . ' ' . $user->password . '<br />';
echo
$user->date_created . ' ' . $user->date_updated;

/* RESULTS
 * 1 Test User Surname Password
 * 2012-11-19 14:27 2012-11-19 14:27
*/