Hola gente, estoy haciendo unas practicas con activeRecord (perdon no tengo acentos) y todo funciona bien salvo al recuperar la id de las tuplas en la bds, se insertan bien los id auto_increment,
pero al recibirlos siempre son todos null. Esta es mi implementaci'on (es un ejercicio nada serio)
// When you create this abstract class, you should thing about what a bee does? what it is commong for all the bees types?
Código:
<?php
//include('php-activerecord/ActiveRecord.php');
use ActiveRecord\Model as ActiveRecordModel;
class db extends ActiveRecordModel {
public function __construct($attributes){
db::initConnect();
parent::__construct($attributes, false);
}
static function initConnect() {
ActiveRecord\Config::initialize(function($cfg) {
$cfg->set_model_directory('src');
$cfg->set_connections(array(
'development' => 'mysql://root:usbw@localhost:3307/bees?charset=utf8'));
});
}
}
Código:
Interface BeesInterface {
public function setType($type);
public function getType();
public function setName($name);
public function getName();
public function setLife($life);
public function getLife();
public function getMiles();
public function setMiles($miles);
}
Código:
class Bees extends db implements BeesInterface {
static $table_name = 'bees';
protected $type;
static $primary_key = 'id';
public $id;
protected $name;
protected $life;
protected $miles;
static $attr_accessible = array('id', 'name', 'life', 'miles');
protected function fly($mts){}
public function setId($id){
return $this->id;
}
public function getId() {
return $this->id;
}
public function __construct($attributes = array()) {
// Fill all the object attributes.
if (!empty($attributes)) {
foreach ($attributes as $attrName => $attrValue) {
$dynamicMethodSet = 'set' .ucfirst($attrName);
$dynamicMethodGet = 'get' .ucfirst($attrName);
// var_dump($method); die;
$this->$dynamicMethodSet($attrValue);
$attributes[$attrName] = $this->$dynamicMethodGet();
}
}
$attributes['life'] = $this->getLife();
parent::__construct($attributes);
}
public function setType($type) {
$this->type = $type;
}
public function getType() {
return $this->type;
}
// protected method, you can call this method without a Bee instance.
protected function initPlay($task) {
// Possible tasks a bee can do it.
switch ($task) {
case 'fly':
return $this->fly($mts); // Getting from the class.
break;
case 'life':
return $this->getLife(); // Getting from the parent class.
break;
case 'miles':
return $this->getMiles(); // Getting from the parent class.
break;
}
}
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
// Bee resurrection.
public function setLife($life) {
$this->life = $life;
}
// Get bee's life.
public function getLife() {
return $this->life;
}
// get bee's miles.
public function getMiles() {
return $this->miles;
}
// set bee's miles.
public function setMiles($miles) {
// Each mile is 10 power points less
$this->setLife($this->getLife() - $miles/10);
return $this->miles = $this->miles + $miles;
}
}
class Queen extends Bees {
// When we create a new object (instance) of this type (Queen), we set its life by default,
// calling the parent class setLife, because $life is a protected property and we cannt call it directly.
public function __construct($attributes = array()) {
$this->setLife(50);
$attributes['type'] = 'Queen';
parent::__construct($attributes);
}
// We are overriding this method.
protected function fly($mts = 5) {
echo "I am the Queen, and I don't need to fly to much";
$newLife = $this->getLife() - ($mts * 5);
$this->setLife($newPower);
}
// Overriding parent setMiles.
public function setMiles($miles) {
// Each mile is 2 power points less
$this->setLife($this->getLife() - $miles/10);
return $this->miles = $this->miles + $miles;
}
}
class Worker extends Bees {
// When we create a new object (instance) of this type (Worker), we set its life by default,
// calling the parent class setLife, because $life is a protected property and we cannt call it directly.
public function __construct($attributes = array()) {
$this->setLife(20);
$attributes['type'] = 'Worker';
parent::__construct($attributes);
}
// We are overriding this method.
public function fly($mts = 5) {
echo "I am flying a lot , because I have to work in many places";
$newLife = $this->getLife() - ($mts * 5);
$this->setLife($newLife);
}
// Overriding parent setMiles.
public function setMiles($miles) {
return $this->miles = $this->miles + $miles;
}
}
Código:
<div class="col-8 col-md-auto">
<h2>Bees availables</h2>
<?php $bees = Bees::all(); var_dump(Bees::connection()->last_query); ?>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Life</th>
<th>Miles</th>
<th>Play</th>
<th>#Id</th>
</tr>
</thead>
<tbody>
<?php foreach($bees as $bee) :// var_dump($bee); die; ?>
<tr>
<td><a href="/bee/<?php $bee->name ?>"><?php echo $bee->name ?></a></td>
<td><?php echo $bee->getType(); ?></td>
<td><?php echo $bee->getLife(); ?></td>
<td><?php echo $bee->getMiles(); ?></td>
<td><a href="/bees/play/<?php echo $bee->id; ?>"><span class="glyphicon glyphicon glyphicon-play-circle" aria-hidden="true">Play</span></a></td> ALWAYS NULL
<td><?php echo $bee->id; ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
LO ACABO DE SOLUCIONAR, ese setter y getter que se me hab'ia escapado para id era lo que me devolv'ia null, adem'as de que he tenido que quitar el public $id, ya funciona!!
Gracias de antemano