EnglishSvenska

Using regular expressions in CakePHP and MySQL

If you want to find something in the database using regular expressions:

$domainNamed = $this->DomainName->find('all', array(
'conditions' => array(
'name REGEXP "^www.i"'
),
'fields' => array(
'name'
),
'contain' => false,
'limit' => 10
));

The code above will find all domain names that start with "www.".

USE INDEX the CakePHP way

Well ok not 100% the cakephp way but better than just using $this->query(...).

I wanted it to look like this:

$this->find('all', array(

'fields' => array('id', 'name'),

'use' => 'index_name'

));

To get this working you need to modify dbo_source. Copy it from cake/libs/model/datasources/dbo_source.php to app/models/datasources/dbo_source.php.

You can download my modified dbo_source.php . I have commented the modified lines with "added". I have only tested this on MySQL. I do not know if it will work on other databases. Also I am not sure if my solution is the best. Please feel free to comment if you have any improvements.

 

To manually modify the dbo_source to support the USE INDEX syntax:

in function read:

/* added */

if (!isset($queryData['use']))

    $queryData['use'] = null;

/* /added */

in function generateAssociationQuery:

    return $this->buildStatement(

    array(

'fields' => array_unique($queryData['fields']),

'table' => $this->fullTableName($model),

'alias' => $model->alias,

'limit' => $queryData['limit'],

'offset' => $queryData['offset'],

'joins' => $queryData['joins'],

'conditions' => $queryData['conditions'],

'order' => $queryData['order'],

'group' => $queryData['group'],

'use' => $queryData['use'] // added

    ), $model

    );

in function buildStatement:

return $this->renderStatement('select', array(

    'conditions' => $this->conditions($query['conditions'], true, true, $model),

    'fields' => implode(', ', $query['fields']),

    'table' => $query['table'],

    'alias' => $this->alias . $this->name($query['alias']),

    'order' => $this->order($query['order'], 'ASC', $model),

    'limit' => $this->limit($query['limit'], $query['offset']),

    'joins' => implode(' ', $query['joins']),

    'group' => $this->group($query['group'], $model),

    'use' => $query['use'] // added

));

in function renderStatement:

    case 'select':

/* added */

if (empty($use)) // added

    return "SELECT {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order} {$limit}";

else

    return "SELECT {$fields} FROM {$table} {$alias} {$joins} USE INDEX ({$use}) {$conditions} {$group} {$order} {$limit}";

/* /added */

break;

New better version

[Update 2012-02-13]

I have updated the code to run better on Eventghost web server. 

New screen shot:

eventghost_web_gui_html5

android_tablet_eventghost

Some screens [old]:

eventghosteventghost2eventghost3eventghost4eventghost5

Video (in Swedish):

http://www.youtube.com/watch?v=UGinP0FXSVA

Instructions:

  1. Put the files on your Eventghost web server. Make sure that you run the latest Eventghost since the earlier versions of the web server does not work very good. 
  2. /js/config.js is for configuring jquery mobile. You can probably keep it as it is.
  3. /index.html is the start page. The rest of the pages are in /docs/controls. 
  4. Visit the web server in your browser. If new actions appear when you click on buttons in the gui then it works. 

For more detailed instructions (in swedish) visit https://sebastiannilsson.com/open-source/eventghost-web-gui/101-instruktioner-foer-hemmaserver-tellstick-nexa-och-ir-saendare

Latest source code can be grabbed from Bitbucket . Or just click here to get the zip.

Lock tables in CakePHP

I needed to do some table locking. Just call the method from the controller to lock and unlock your tables. The code below is only tested with MySQL and InnoDb tables. Put it in app_model.php

{code }

<?php

class AppModel extends Model {

    function lockTableRW() {

$this->lockTable('READ');

$this->lockTable('WRITE');

    }

    function lockTable($type='READ') {

$dbo = $this->getDataSource();

$dbo->execute('LOCK TABLES '.$this->table.' '.$type);

    }

    function unlockTables() {

$dbo = $this->getDataSource();

$dbo->execute('UNLOCK TABLES');

    }

}

 

 

Kul! PC för Alla tipsar sina läsare om Spotifyspindeln

pcforallaAlltid roligt när ens sajter blir omnämnda på andra ställen. PC för alla skrev några ord om den under sina surftips. Resultatet blev att spotifyspindeln under en kort period inte gick att nå på grund av överbelastning. Får eventuellt ta och uppgradera servern om det blir nödvändigt. Här är artikeln: http://www.idg.se/2.1085/1.373293/musikradion-utan-reklam

Spotifyspindeln på Feber.se

Effekten av att bli uppmärksammad av Feber blir ofta stor. Detta fick vi uppleva när vår sajt spotifyspindeln.se blev publicerad där som en artikeln under Webb-kategorin. På en dag ökade besöksantalet till 1 000 besökare vilket är en bra bit över det normala. Här är artikeln: http://feber.se/webb/art/200806/spotifyspindeln_skapar_spellis/

Kort om Feber är att de publicerar artiklar om sånt de finner intressant för tillfället. Besökarna kan därefter höja eller sänka temperaturen till artikeln beroende på om de tycker det är en het nyhet eller inte (och därav namnet feber). Just nu ligger artikeln om Spotifyspindeln på 41,7 grader. Ganska hög feber med andra ord. Gå gärna in på artikeln och höj temperaturen lite till om du gillar tjänsten!

spotifyspindeln_feber

Performance improvements

Performance improvements have been made. Some of these are:

  • Updated jQuery Mobile to latest version
  • Better javascript for events
  • Faster ajax calls
  • Cache manifest
  • + bug fixes

For example, it is now possible to navigate XBMC with much less lag.

Visit project on Bitbucket to download code

My Eventghost xml file

Sliders for volume and light

I have updated Eventghost Web Gui with sliders for sound volume and light. 

This is still a little buggy but works quite well on my phone. Test it and leave a comment describing how it works for you!

Visit project on Bitbucket to download code

eventghoast_dynamic_web_server_variable_1eventghoast_dynamic_web_server_variable_2

Eventghost Web Gui

[Updated. See https://sebastiannilsson.com/open-source/eventghost-web-gui for new version]

This a Web gui for Eventghost built in jQuery Mobile. It uses an ajax function to call Eventghost events to avoid sending multiple requests on page refresh. You will need the web server plugin to get this working.

Building your own gui is quite easy even if you do not know that much html. Just copy the example and modify it. Events are set by the title attribute. An ajax function handles sending events so you don't have to. Style your gui with the jQuery api. I have only tested this on Android but since it is built with jQuery Mobile I think it will work excellent on Iphone and Ipad aswell. 

Most of the text in the example is in Swedish since it is the configuration I use myself. The example is just to demonstrate how to build a gui and you have to modify it to suit your Eventghost configuration.

Visit project on Bitbucket to download code

Some images:

eventghost_web_gui_starteventghost_web_gui_tveventghost_web_gui_tv_optionseventghost_web_gui_lightseventghost_web_gui_xbmc

On my HTC Desire (Android):

eventghost_web_server_1eventghost_web_server_2eventghost_web_server_3eventghost_web_server_4

Skapa Spotifylistor från radiokanaler

 

spotifyspindelnJag har gjort en webbplats där man kan skapa Spotifylistor från radiokanaler. Idén är att man på så sätt ska kunna lyssna på radio utan radioreklam och massa prat. Det som radiokanaler spelas blir det en playlist av.

 

Till Spotifyspindeln

 

Håller på att ta fram en lista över radiokanaler som kan vara intressanta att utöka webbplatsen med. Om du har några förslag är det bara att skriva som en kommentar eller kontakta mig.

 

Det som behövs för att jag ska kunna lägga in en radiokanal är en länk till en sida där det någonstans står vad som spelas just nu. Ge mig en länk till vad kanalen spelar så kommer den troligtvis upp. Exempel: http://mobil.sr.se/rightnow.aspx?unitid=1607