Source for file PMO_MyObject.php
Documentation is available at PMO_MyObject.php
* This file contains the PMO_MyMapObject class which implements
* the generic PMO_Object that represents a tuple in database.
* This file is part of the PhpMyObject project.
* For questions, help, comments, discussion, etc., please join our
* forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
* or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
* PhpMyObject 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, either version 3 of the License, or
* (at your option) any later version.
* 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/>.
* @author Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
* @link http://pmo.developpez.com/
* @since PhpMyObject v0.1x
* @copyright Copyright (C) 2007-2008 Nicolas Boiteux
* @license GPLv3 {@link http://www.gnu.org/licenses/gpl}
/** requires the interface */
require_once(dirname(__FILE__ ). '/PMO_Object.php');
* This class describe a generic PMO_Object that represents a tuple
* from the database. It's a model class (the M part of the MVC design pattern)
* that enables you to create, read, update and delete data (CRUD).
* This class can be extended by adding methods or fields
* and load dynamically via the PMO_Table mecanism
* @see class_film_actor.php
* @tutorial PhpMyObject/PhpMyObject.Quickstart.pkg#using.pmo_myObject a simple introduction to PMO_MyObject
* @tutorial PhpMyObject/Manual.pkg#PMO_MyObject the manual chapter on PMO_MyObject
* holds the related PMO_Table object
* holds the object attributes
* boolean flag indicating if this object is a new one or
* has already been loaded through the {@link load()} method
* the constructor takes a PMO_Table argument and sets ifself up
* up so {@link factory()} will be able to create and return
* @param object $table a PMO_Table object
* this internal factory will instanciate either PMO_Object based
* on an extended class in your class_loader directory or a generic
* @param object $table a PMO_Table object
$classname = $table->getClassname();
$object = new $classname($table);
* returns an object conforming to the database table schema
* This factory calls an internal factory that is used to load your own
* class if it exists in the class_loader directory and is referenced in
* the property $table_classname in the corresponding
* PMO_Table/PMO_Table_Classname.php file.
* @param string $tablename the database table name to use
* for building the PMO_Object object
public static function factory($tablename){
* Returns a reference to the PMO_Table object on which this object is based
* PMO_Table describe the structure of the table that was used to build
* the object data structure (column names, primary keys, ..)
* @throws Exception if the PMO_Table is undefined
throw new Exception("Error: Object is undefined");
* return the data structure of the PMO_Object
* array is attribute=>value
throw new Exception("Error: No data found in object");
* return an array wich contains
* all the names of attributes of PMO_Object
foreach($attributes as $attributename=> $attributevalue)
$array->append($attributename);
* retrieve the value of an attribute
* this function also cleans the escape chars
* with a simple stripslashes
* @param sting $attribute
public function get($attribute){
throw new Exception("Error: Attribute ". $attribute. " doesn't exist");
* Set the value of an attribute of the data structure
* this function already escape strange char with
* data structure is an array
* @param string $alias column alias
* @param mixed $value value to set the attribute to
* @return bool always TRUE
public function set($attribute, $value){
throw new Exception("Error: Attribute ". $attribute. " value is undefined");
* export the data structure of an object in a xml format stream
* <attributename>attributevalue</attributename>
public function toXml($encoding){
$out = "<?xml version=\"1.0\" encoding=\"$encoding\"?>\r";
$out .= "<attributes>\r";
$out .= "<{$key}>$value</{$key}>\r";
$out .= "</attributes>\r";
* @param string $attributename
* @param mixed $attributevalue
public function __set($attributename, $attributevalue) {
$this->set($attributename, $attributevalue);
* @param string $attributename
public function __get($attributename) {
return $this->get($attributename);
* define the object as new, not already present in the database
* This flag is used to define when we do an insert or an update
public function setNew($flag){
throw new Exception("Error: New flag should be a boolean");
* return the flag used to define if the objet is
* already present in database table
* Loads a data row from the database table using its primary key
* and fills the object attributes
* Primary key needs to be set beforehand
* Deletes the corespondgng data row from the database table
* primary key needs to be set beforehand to delete the right tuple
* Save this object into the corresponding database table
* If this is a new object, it will be inserted. Otherwise
* an update will be performed.
* updates the corresponding data row into the database table
* inserts a new data row into the database table
* Fields must be filled beforehand.
|