Welcome Guest, Not a member yet? Register   Sign In
Doctrine Default Primary Key Problem (Again)
#1

[eluser]01010011[/eluser]
Hi,

Should I change all of my uniquely-named MySQL database primary keys to 'id' to avoid getting errors related to Doctrine's default primary key set in the plugin 'doctrine_pi.php'?

To further elaborate, I am getting the following reoccurring error, this time after trying to login to my login page:

Quote:SQLSTATE[42S22]: Column not found:
1054 Unknown column 'u.book_id' in
'field list'' in...

I suspect the problem resides at a MySQL table used for my login, of which has a primary key called

Quote:id

Marc B originally solved an identical problem for me in this post

Code:
http://stackoverflow.com/questions/2702229/doctrine-codeigniter-mysql-crud-errors

when I had the same problem with a different table within the same database. Following his suggestion, I changed the default primary key located at

Quote:system/application/plugins/doctrine_pi.php

from 'id' to 'book_id':

Code:
<?php
    // system/application/plugins/doctrine_pi.php
    
    ...
    
    // set the default primary key to be named 'id', integer, 4 bytes
    Doctrine_Manager::getInstance()->setAttribute(
        Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
        array('name' => 'book_id', 'type' => 'integer', 'length' => 4));
and that solved my previous problem. However, my login page stopped working.

So what is the safe thing to do? Change all of my primary keys to 'id' (will that solve the problem without causing some other problem I am not aware of). Or should I add some lines of code in doctrine_pi.php?
#2

[eluser]dmorin[/eluser]
This doesn't make sense to me. Setting a default primary key is ok for Doctrine migrations and being able to create new tables, but it shouldn't at all be impacting how your existing tables operate. How are you defining your models? Are you using YAML, reverse generation from the DB, or are you coding them manually? Those models should have a setTableDefinition() function and within that, you define your columns. One column should have 'primary'=>true in it which tells the model which column is the primary key. If that's set correctly, it shouldn't matter what your default is.

Maybe try posting your setTableDefinition() function for the model in question along with the current db schema for that table and we'll see if anything looks off. Also, I would NOT set book_id to the default id field since that's only valid for one table yet you're setting it globally.
#3

[eluser]dmorin[/eluser]
Alright, after looking at your stack overflow post, I see one problem. You're not telling doctrine which field is the primary key, only that the field exists.

Code:
$this->hasColumn('book_id');
Should be:
Code:
$this->hasColumn('book_id', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => true,
             'primary' => true,
             'autoincrement' => true,
             'length' => '4',
             ));

Of course, some of those params may need to be adjusted, for example, if you id column is signed (which it shouldn't be) but Doctrine needs all of this info to work correctly. I would highly recommend allowing doctrine to reverse engineer your db so the models are setup correctly instead of you doing them manually. Run something like:
Code:
$options = array(
                "generateBaseClasses" => true,
                "generateTableClasses" => true
        );

Doctrine::generateYamlFromDb(SCHEMA_DIRECTORY.'schema.yml', array('doctrine'));
Doctrine::generateModelsFromYaml(SCHEMA_DIRECTORY.'schema.yml', MODELS_DIRECTORY, $options);

This would rely on the constants above being defined so adjust as necessary.




Theme © iAndrew 2016 - Forum software by © MyBB