[Solar-talk] autocreate table

Jeff Surgeson solar at 3hex.com
Thu Aug 24 05:13:30 PDT 2006


> > Can anyone let me know how to autocreate tables. I am getting the below
> > given error when i try to instantiate my Model.
>
> Uh... I don't know either when the autocreate process is triggered...

As far as I know when you instantiate a data model,

$this->_user = Solar::factory('Vendor_Model_User');

Solar will auto create that table if it does not exist, the table created is 
based on the _setup() in your model file

protected function _setup()
{
        // Table name
        $this->_name = 'users';

        // COLUMNS
        // username of the owner
        $this->_col['owner_handle'] = array(
            'type'    => 'varchar',
            'size'    => 64,
            'require' => true,
	);

etc etc

and if you want to insert a record when it is first created then just add a 
_postCreate() at top of model file

/**
 * Creates the admin user after creating
 * the table schema
 *
 * @return bool True on successful insert
 */
protected function _postCreate()
{
    $user = array(
        'owner_handle'  => 'email at example.com',
        'user_pass'     => md5('securepassword'),
    );

    $this->insert($user);
    return true;
}

Thats the way I do it, hope it helps a bit.

-- 
Jeff Surgeson / South Africa


More information about the solar-talk mailing list