Stephen’s dev blog

Custom SQL in symfony 1.2.3

Posted by: Stephen Gray on: June 29, 2009

Quick one. A small snippet for performing custom SQL queries using symfony 1.2.3. The method to do this has varied slightly with the different symfony releases. The method found on the symfony site almost has a good example, this is just adding a bit to it (looping);

    $conn = Propel::getConnection();
    $query = 'SELECT * FROM `my_table`;';

    $statement = $conn->prepare($query);
    $statement->execute();

    while ($rowObj = $statement->fetch(PDO::FETCH_OBJ))
    {
        echo var_dump($rowObj);
    }

Stephen.

jQuery wildcard selectors – update

Posted by: Stephen Gray on: April 24, 2009

Hello all,

On my first post on this blog I posted a small snippet to use a small regex pattern in jQuery selectors.

You can find the post here: http://colourgray.wordpress.com/2008/08/05/jquery-wildcard-selectors/

I was reading through some comments and posts on some incoming links and it seems it wasn’t working for anyone anymore. I just tested it myself and unfortunately it’s true (at least with with jQuery v1.3.1). However! All is not lost, there is another method which I’ve tested from one of the incoming links to that post which you can find here:

http://ropox.net/archives/1081

Cheers ropox!

Stephen.

Symfony “Database “” does not exist.” error

Posted by: Stephen Gray on: March 2, 2009

Hello,

It’s definitely been too long since I posted here! I will try and post more regularly over the coming months…

Firstly, we came across this issue after writing a number of plugins for a symfony project. Each of these plugins had their own schema.yml files and therefore we didn’t need our own custom schema.yml file at that time.

Because of this, our config/schema.yml file looked like this:

    propel:

Pretty, right? When we ran symfony propel:build-sql (after the other usual build tasks) we got the error:

    Database "" does not exist.

In symfony’s glorious vibrant red error container. It seems when your schema file looks like ours did above, symfony generates the sqldb.map file for that file without a database name, our data/sql/sqldb.map file looked similar to this:


    # Sqlfile -> Database map
    ...
    lib.model.schema.sql=propel
    plugins.pluginName.lib.model.schema.sql=propel
    plugins.pluginName2.lib.model.schema.sql=propel
    generated-schema.sql=propel
    generated-pluginName-schema.sql=propel
    generated-pluginName2-schema.sql=propel
    ...

The quick fix, especially if you don’t want to have to add a custom table in your schema just to get round this, is to simple add ‘propel’ onto the end of the line that is missing it.

Hopefully there’s a proper fix that I so far haven’t been bothered to find…

I’ve just made a change to a symfony project’s schema.yml file and tried to rebuild the model and came across this error message:

Unable to parse default value as date/time value: ‘0000-00-00 00:00:00′

Which was stopping the build. After looking at various places, it seems this is a problem with Creole, (propel’s DBAL).

Before PHP v5.2.4, you could do a strtotime on a value of 000-00-00 00:00:00 and you would get a weird date, something in 1999. Not sure why :S Since the 5.2.4 update however this bug has been fixed and you will now get boolean false returned.

The symfony plugin sfGuard, which is widely used, uses 0000-00-00 00:00:00 as a default value in it’s schema for some fields and this will now cause problems. To fix this, edit sfGuard’s schema and change those default values to something like 1970-01-01 and that should do the trick.

Unfortunately Creole isn’t actively maintained anymore so it’s not likely to be updated soon to fix this.

jQuery loading remote/external javascript files using getScript()

Posted by: Stephen Gray on: September 22, 2008

I’ve been working over the weekend on a site and I got to the part of writing the client side MVC using jQuery (because I love the idea now!).

I found out that jQuery has a nice little method for loading external scripts into the page at run time called getScript(). It basically gets the contents of the external file using an AJAX request and then eval()s the code at runtime, which seems like a perfect way of doing it. I reckon it’s much better than appending new <script> tags to the <head> tag as well.

The only issue is that it can take some time to load an external file so you can’t always run functions from within that file straight away. Thankfully there is a workaround for this using another jQuery method. You basically set all AJAX request to be synchronous before you run getScript() and then set AJAX requests to be asynchronous again after the script is loaded. If you end up loading like 20 files at the beginning of a page load this could take a while but hopefully you won’t be doing that :P

$.ajaxSetup({async: false});
$.getScript(MVCRoot+fileName+'.js');
$.ajaxSetup({async: true});

And here’s some more info on this lovely little function :)