Tuesday, July 24, 2012

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

After replying to Ralf, I dropped everything in my 'test' database, and went back to the code I had before, " $myModel->save(...)".  Now, I'm seeing something else:
  • The page displays a PDOException " SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 1 "
  • The error_log displays the MissingActionException.
I find it frustrating that:
  • The message for PDOException doesn't tell me what query was run (since it was composed inside Cake's core).
  • The output from MissingActionException shows what action is missing, "Action AppController::webroot() could not be found", but not where the request got generated and sent

On Tuesday, July 24, 2012 8:25:33 AM UTC-4, lsiden wrote:
Stuff, more stuff, and stuff to change.  I want to avoid cluttering the post with too much detail that will make people's eyes glaze over.  

What I should do when I have time is clone the database schema, eliminate anything non-relevant, repopulate it with "lorem ipsum" to satisfy my client that I'm not disclosing anything proprietary, and see if I can recreate it.  Then I can post it on Github.

I did find something suspicious that make me think that this is related to fixtures: My test pulls in six fixtures, but when I look at my 'test' database, only one fixture table has been created, the one corresponding to "stuff_to_change".  When I open it's class file, it's the only one that I didn't define "public $records = ...".  


CakePHP performs the following during the course of a fixture based test case:
Creates tables for each of the fixtures needed.
Populates tables with data, if data is provided in fixture.
Runs test methods.
Empties the fixture tables.
Removes fixture tables from database.

So it makes me suspicious that a table still remains in the test database after the test runner is finished.

Every time I run a test, I've been seeing the following stack trace in my error_log:

2012-07-23 19:37:52 Error: [MissingActionException] Action AppController::webroot() could not be found.
#0 /opt/cakephp_2.0/lib/Cake/Routing/Dispatcher.php(186): Controller->invokeAction(Object(CakeRequest))
#1 /opt/cakephp_2.0/lib/Cake/Routing/Dispatcher.php(161): Dispatcher->_invoke(Object(AppController), Object(CakeRequest), Object(CakeResponse))
#2 /opt/csw/apache2/share/htdocs/kaos/app/webroot/index.php(93): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#3 {main}

I already created a ticket for this, but kept going because I didn't think it was related.  The page comes up normally after a test is run.  Now I wonder if this exception is hiding some other problem.

On Tuesday, July 24, 2012 2:22:51 AM UTC-4, Ralf Rottmann wrote:
What is in the 'Stuff', 'MoreStuff' and 'StuffToChange' arrays?

On Tuesday, July 24, 2012 2:05:40 AM UTC+2, lsiden wrote:
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

No comments: