Monday, July 23, 2012

saving HABTM data in a fixture - have to resort to Model::query() to get it done

I created a component test that modifies and saves a model with HABTM associated data.  

Before I upgraded to CakePHP 2.0, I tried to accomplish this with unbindModel() all over my code, but wanted to try something a little cleaner albeit less efficient.  I spent hours reading  http://book.cakephp.org/2.0/en/models/saving-your-data.html and wanted to try doing it the "Cake" way: 

$data = array(
  'User' => array(
    'id' => 99,
    ...
  ),
  'Stuff' => array( ... some stuff ...),
  'MoreStuff => array( .. more stuff ...),
  'StuffToChange' => array( ... this is the stuff I added a record to ...)
);

When I look at the query log, Cake generated queries to delete Stuff, MoreStuff, and ReallyImportantStuff, but only does inserts into StuffToChange.

I'm wondering if this is somehow related to the fact that I'm using fixtures in my test.  I suppose I could clone the real database and try it on that to see if its a fixture problem.  (I'm too exhausted now to try anything.  I need a break!)  

So in order to make progress I had to give up for now and do it the "brute force" (but much more effiicient way):

$model->query("insert into stuff_to_change where user_id=99 and something_id=xxx")

I suppose Cake purists will tell me that this is wrong and could create problems in the future if I were to migrate the data to a different database platform, for example, but I wasted half a day trying to get this to work and I can't afford to spend much more time when I have a functioning workaround.  Even if it turns out to be an issue that will only happen when I use my fixtures, I want it to work the same way in a unit test as it works in the field.

Anyone else having this problem in Cake 2.x?  (I'm actually in the master branch from the Git repo - yeah, I'm bleeding edge now).

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate