[Solar-talk] sql_model auto-create

Jeff Surgeson solar at 3hex.com
Sat Feb 10 02:16:58 PST 2007


Have been playing with the new sql_model stuff, and guess what? I cant make it 
auto create a table if it does not exist, whats new with me! :-)

If table exists then calls to fetchOne() or fetchAll() work ok so I know I am 
instantiating the sql_model object correctly and using it correctly, so it 
can only be the way I have set up my table definitions etc.
As simple data model:

class Bulldog_Model_Nodes extends Solar_Sql_Model {
    protected function _setup()
    {
        $this->_table_name = 'nodes';
        
        $this->_table_cols['id'] = array(
            'type'    => 'int',
            'autoinc' => true,
        );
    
        $this->_table_cols['email'] = array(
            'type'    => 'varchar',
            'size'    => 255,
        );

        $this->_idx['id'] = 'unique';
    }
}

$this->nodes = Solar::factory('Bulldog_Model_Nodes');
$this->node = $this->nodes->fetchOne(1);

Am I correct in saying with the new model class only uses auto-increment and 
not sequences any more at all? (bummer Paul I know how much you liked 
them) :-)

In one of your emails you state "Reads the schema from the database, creates 
the schema if it  doesn't exist" my question, "schema from database" must 
mean from data model as in Vendor_Model_TableName, or am I missing something 
here.

If table does not exist in your database then when instantiating your data 
model the new model class should create the table as defined by $_table_cols, 
correct? 

Using the schema above with no existing "Nodes" table, the call to 
$this->nodes = Solar::factory('Bulldog_Model_Nodes'); 
generates a sql error for me.

Fatal error: Uncaught exception 'Solar_Sql_Adapter_Exception_QueryFailed' 
class::code 'Solar_Sql_Adapter_Mysql::ERR_QUERY_FAILED' with 
message 'ERR_QUERY_FAILED' information array ( 'pdo_code' 
=> '42000', 'pdo_text' => 'SQLSTATE[42000]: Syntax error or access violation: 
1064 You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near \'NULL, email 
NULL ) TYPE=InnoDB\' at line 2', 'host' => '127.0.0.1', 'port' => 
NULL, 'user' => 'root', 'name' => 'test_db', 'stmt' => 'CREATE TABLE nodes ( 
id NULL, email NULL ) TYPE=InnoDB', 'pdo_trace' 
=> '#0 /var/www/localhost/htdocs/libmod/Solar/Sql/Adapter.php(390): 
PDOStatement->execute(Array) 
#1 /var/www/localhost/htdocs/libmod/Solar/Sql/Adapter.php(1041): 
Solar_Sql_Adapter->query(\'CREATE TABLE no...\') 
#2 /var/www/localhost/htdocs/libmod/Solar/Sql/Model.php(1897): 
Solar_Sql_Adapter->createTable(\'nodes\', Array) 
#3 /var/www/localhost/htdocs/libmod/Solar/Sql/Model.php(1555): Solar_Sql_Mod 
in /var/www/localhost/htdocs/libmod/Solar.php on line 546

Is this me or is there still some work to do on the auto creation code?

-- 
...........::::::...........
Jeff Surgeson / South Africa


More information about the solar-talk mailing list