Finally, I've decided to give it a try - Doctrine!
It looks nice, but ...
I tried to create a simple base CRUD Model (CodeIgniter):
Code: Select all
/**
* @property string $tableName
* @property Doctrine_Collection $collection
* @property Doctrine_Record $record
* @property Doctrine_Table $table
*
*/
class CRUD_Model extends Model
{
private $recordClass= null;
private $table = null;
private $collection = null;
private $record = null;
public function __construct($recordClass = '')
{
parent::__construct();
$this->setRecordClass($recordClass);
}
public function setRecordClass($recordClass)
{
if (!empty($recordClass))
{
$this->recordClass = $recordClass;
$this->table = Doctrine_Manager::connection()->getTable($this->recordClass);
}
}
/**
* Load the collection of objects.
*
* @access public
* @return bool
*/
public function load()
{
$query = Doctrine_Query::create()
->from($this->recordClass);
$this->collection = $query->execute();
}
/**
* Return the object collection
*
* @access public
* @return Array
*/
public function get()
{
return $this->collection->toArray();
}
/**
* Add an object to the collection.
*
* @access public
* @param mixed $data
* @return bool
*/
public function add($data)
{
$this->record = $this->table->create();
// $this->record->assignIdentifier(null);
$this->record->synchronizeWithArray($data);
$this->record->save();
}
/**
* Update the object in the collection.
*
* @access public
* @return bool
*/
public function update($data)
{
$this->record = $this->table->create();
$this->record->assignIdentifier($data['id']);
$this->record->synchronizeWithArray($data);
$this->record->save();
}
/**
* Remove an object from the collection
*
* @access public
* @return bool
*/
public function remove($data)
{
$this->record = $this->table->create($data);
$this->record->assignIdentifier($data['id']);
$this->record->delete();
}
}Code: Select all
abstract class RBaseCountry extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('country');
$this->hasColumn('id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => 1,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('name', 'string', 25, array(
'type' => 'string',
'length' => 25,
'fixed' => true,
'primary' => false,
'notnull' => true,
'notblank' => true,
'autoincrement' => false,
));
}
public function setUp()
{
parent::setUp();
$this->hasMany('RProvince', array(
'local' => 'id',
'foreign' => 'country_id'));
}
}Also, I tried to get the PK from the record by using record::identifier() but it was empty (before the assignIdentifier() call )?!? I'd suppose that table definition is enough to populate this array (at least it key values), especially when Table::create() is used ...
And last - if I try to use $this->record->assignIdentifier(null) in add() (so, that I could use a single save() method instead of two separate update/add methods) no creation of DB record happens.
Anybody (~josh
I really like how Doctrine works, but these things really annoyed me. I hope, it's my mistake somehow...