Welcome Guest, Not a member yet? Register   Sign In
Database Forge add_column query problem
#1

[eluser]GreenDude[/eluser]
Hi.

I'm building an application that processes a xml file and creates a table accordingly.

The problem is that some columns have names that include spaces or dots, and the add_column function splits the name inside the query.

For example, if I try adding a field called "What the hell", I get the following error:

Code:
Error Number: 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 'the `hell` VARCHAR(225) NOT NULL' at line 2

ALTER TABLE `info` ADD `what` the `hell` VARCHAR(225) NOT NULL

Here's how my array looks like:

Code:
Array
(
    [what the hell] => Array
        (
            [type] => VARCHAR
            [null] =>
            [constraint] => 225
        )

)

And here's the chunk of php that does the work:


Code:
$fields = array();
$fields[$this->nume] = array( 'type' => strtoupper($this->tip), 'null' => FALSE );

if ( $this->restrictie != NULL  ) $fields[$this->nume]['constraint'] = $this->restrictie;
else if ( $this->tip == 'varchar' ) $fields[$this->nume]['constraint'] = 225;

if ( $this->optional ) $fields[$this->nume]['null'] = TRUE;

$this->dbforge->add_column('info', $fields);

Any idea how I could make the query use the whole name?

GreenDude
#2

[eluser]GSV Sleeper Service[/eluser]
you could try putting the table name in quotes, or replacing the spaces with underscores before passing it to add_column.

I was bored so I traced the code back to a function called _protect_identifiers in [database]_driver.php, it looks like this is where the backticks are being added, this comment caught my eye.

Code:
// This function may get "item1 item2" as a string, and so
// we may need "`item1` `item2`" and not "`item1 item2`"
#3

[eluser]GreenDude[/eluser]
In the end I used a classic db->query to get around it, and it seems to work.

Thanks for the tip with the comment, maybe they'll condition the backticks in the future. Or maybe not.

Now I find myself with another problem... is there a predefined helper for processing the names I use in the columns and returning a "nicename" for use inside input element names?

The underscore is a solution, but if there's something already implemented please give me a hint.

Cheers,
GreenDude
#4

[eluser]GSV Sleeper Service[/eluser]
url_title will do the job - http://ellislab.com/codeigniter/user-gui...elper.html
#5

[eluser]GreenDude[/eluser]
Thanks again. It works perfectly.


Cheers,
GreenDude




Theme © iAndrew 2016 - Forum software by © MyBB