jeudi 30 juillet 2015

Cakephp Fixtures - record definition with DB expression

So I've recently started writing tests for my Cake application and I've run into a bit of a snag.

I prefer defining my test data using the $records variable, but one of my tables has a MySQL Point column which is a data type not supported by Cake.

With the normal storing and retrieval I'm able to use a DB expression in the beforeSave of the model to convert it to a usable format:

private function prepareGeoData($data){
    $db = ConnectionManager::getDataSource($this->useDbConfig);
    return db->expression("GeomFromText('POINT(" . $data['x'] . " " . $data['y'] . ")')");
}

While maybe not the best solution, this has been working. My problem is that I can't seem to get DB expressions to work with fixtures.

I've read the docs, particulary this part about Dynamic Data and Fixtures, but when I define my record as so:

 public function init(){
    $db = ConnectionManager::getDataSource('default');
    $this->records = array(
        array(
            'id' => 1
            ,'name' => 'Point 1'
            ,'point' => $db->expression("GeomFromText('POINT(18 36)')")
        )
    );
    parent::init();
)

I got the following error:

Catchable fatal error: Object of class stdClass could not be converted to string in ...\lib\Cake\Model\Datasource\DboSource.php on line 2927

It clearly doesn't like the object returned by $db->expression, but I can't think of any other way to get the data properly inserted...

Any insights are appreciated!

using Cake 2.3

Aucun commentaire:

Enregistrer un commentaire