Wednesday, April 23, 2014

Re: Spaces in Field Names

Joe,

Thanks for doing this. I came up with a much more hackish temporary fix that I absolutely refuse to post. This is great.

I am using MySQL for testing and MSSQL for production, so I will create new datasources for both. I'm hoping you post this back to GitHub as a pull request so it makes it into future releases.

I know none of us would ever use spaces in field names if we weren't stuck with someone else's schema. In my case, it's a print-industry ERP system that goes for multi $100,000s. Chances of me modifying their schema - or getting them to change for me - are nil. The only solution is to accommodate their poor design.

Thanks again,
Dale

On Tuesday, April 22, 2014 9:52:41 AM UTC-5, Joe Trums wrote:
Not sure if you found the workaround yet. I was having the same issue, trying
to retro-fit an SQL Server database (with bad naming conventions) to a
CakePHP application. The issue is occurring in the regex checks in
*DboSource::name()*. The fix I came up with was to create a custom
*Datasource* (as to not modify the core lib). I copied the Sqlserver.php
(probably Mysql in your case) from /lib/Cake/Model/Datasource/Database/ -to-
/app/Model/Datasource/Database/. I renamed the file and class to /SQLSRV/. I
copied the method *DboSource::name()* to the bottom of SQLSRV ( as this will
now overwrite the parent method ) and made the following change to the last
regex test in the method:

                if (preg_match('/^[\w-_\s]*[\w-_]+/', $data)) {
                        return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote .
$data . $this->endQuote);
                }
                return $this->cacheMethod(__FUNCTION__, $cacheKey, $data);

Became ->

        if (preg_match('/^[\w-_\s]*[\w-_]+/', $data)) {
            if( strpos( $data , '.' ) !== FALSE ){
                $items = explode('.', $data);
                return $this->cacheMethod(__FUNCTION__, $cacheKey,
                    $this->startQuote . implode($this->endQuote . '.' .
$this->startQuote, $items) . $this->endQuote
                );
            }
            return $this->cacheMethod(__FUNCTION__, $cacheKey,
$this->startQuote . $data . $this->endQuote);
        }

Finally I updated the database.php in config to use the new
*Database/SQLSRV* datasource.



--
View this message in context: http://cakephp.1045679.n5.nabble.com/Spaces-in-Field-Names-tp5717644p5718006.html
Sent from the CakePHP mailing list archive at Nabble.com.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: