<?php
require_once 'rb.php';
class TimeMachineItem {
public $name;
public $old;
public $new;
public function __construct($name, $old, $new) {
$this->name = $name;
$this->old = $old;
$this->new = $new;
}
}
class TimeMachine {
public $tabela;
/** @var TimeMachineItem[] */
protected $itemList = [];
public function __construct($tabela) {
$this->tabela = $tabela;
}
public function add_item($item) {
$this->itemList[] = $item;
}
public function count_item() {
return count($this->itemList);
}
}
class Model_Base extends RedBean_SimpleModel {
public function after_update() {
$timeMachine = new TimeMachine(get_class($this));
foreach ($this->bean as $k => $v) {
if ($this->bean->hasChanged($k)) {
$timeMachine->add_item(new TimeMachineItem($k, $this->bean->old($k), $this->bean[$k]));
}
}
if (!empty($timeMachine->count_item())) {
echo '<p>changed</p>';
echo print_r($timeMachine, true);
}
}
}
class Model_Cliente extends Model_Base {
}
class Model_Participante extends Model_Base {
}
class Model_Boleto extends Model_Base {
}
define('DBHOST', '127.0.0.1');
define('DBNAME', '');
define('DBUSER', '');
define('DBPASS', '');
R::setup('mysql:host=' . DBHOST . '; dbname=' . DBNAME, DBUSER, DBPASS);
R::freeze();
R::setAutoResolve(true);
echo '<pre>';
// original cliente_id = 580
$p = R::load('participante', 1);
$p->cliente = R::load('cliente', 1);
/* ok, detect change at cliente_id to 1 */
R::store($p);
/* ok, detect change at cliente_id to 290 */
$p->cliente = R::load('cliente', 290);
R::store($p);
/* NOT ok, does not detect change at cliente_id to 580 */
$p->cliente = R::load('cliente', 580);
R::store($p);