Anorm Another ORM logo Anorm Another ORM

Create and Update

Step 1. Define a model class

use Anorm\DataMapper;
use Anorm\Model;

class SomeTableModel extends Model {
    public function __construct(Anorm $anorm)
    {
        parent::__construct($anorm->pdo, DataMapper::createByClass($anorm->pdo, $this));
        $this->_mapper->mode = DataMapper::MODE_DYNAMIC; // Optional. Enables dynamic mode to alter the table to suit the model.
    }

    /** @var integer The primary key */
    public $id;

    /** @var string Useful documentation about 'name' for intellisense */
    public $name;

}

Step 2. Use it to create a record in the database.

$model = new SomeTableModel(Anorm::use('mydata'));
$model->name = 'bob';
$model->write();

Delete

$id = 3; // Likely passed on via GET or POST
$model = new SomeTableModel(Anorm::use('mydata'));
$model->_mapper->delete($id);