Skip to content

Commit

Permalink
Added Model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Ems committed Aug 11, 2012
1 parent 55bb2fe commit d230488
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Model class for a WordPress.
*
* @author Mike Ems
* @package Mvied
*/
class Mvied_Model {

protected $_post;

public $ID;

public $name;

public function __construct( $id ) {
if ( ! isset($id) ) {
return $this;
}

$this->_post = get_post($id);
$this->ID = $this->_post->ID;
$this->name = $this->_post->post_title;

$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($properties as $property) {
$property = $property->getName();
if ( !isset($this->$property) ) {
$this->$property = get_post_meta($this->ID, $property, true);
}
}
}

public function __get( $property ) {
return get_post_meta($this->ID, $property, true);
}

public function getPost() {
return $this->_post;
}

public function load( $array = array() ) {
foreach($array as $key => $value) {
if ( property_exists($this, $key) ) {
$this->$key = $value;
}
}
}

public function save() {
$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($properties as $property) {
$property = $property->getName();
if ( strpos($property, '_') !== 0 ) {
update_post_meta($this->_post->ID, $property, $this->$property);
}
}
}

}

0 comments on commit d230488

Please sign in to comment.