Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
saving HABTM data in a fixture - have to resort to Model::query() to get it done
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
lsiden  
View profile  
 More options Jul 23 2012, 8:05 pm
From: lsiden <lsi...@gmail.com>
Date: Mon, 23 Jul 2012 17:05:40 -0700 (PDT)
Local: Mon, Jul 23 2012 8:05 pm
Subject: 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).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ralf Rottmann  
View profile  
 More options Jul 24 2012, 2:22 am
From: Ralf Rottmann <m...@ralf.me>
Date: Mon, 23 Jul 2012 23:22:51 -0700 (PDT)
Local: Tues, Jul 24 2012 2:22 am
Subject: Re: saving HABTM data in a fixture - have to resort to Model::query() to get it done

What is in the 'Stuff', 'MoreStuff' and 'StuffToChange' arrays?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lsiden  
View profile  
 More options Jul 24 2012, 8:25 am
From: lsiden <lsi...@gmail.com>
Date: Tue, 24 Jul 2012 05:25:33 -0700 (PDT)
Local: Tues, Jul 24 2012 8:25 am
Subject: Re: saving HABTM data in a fixture - have to resort to Model::query() to get it done

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 = ...".  

According to http://book.cakephp.org/2.0/en/development/testing.html:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lsiden  
View profile  
 More options Jul 24 2012, 9:02 am
From: lsiden <lsi...@gmail.com>
Date: Tue, 24 Jul 2012 06:02:15 -0700 (PDT)
Local: Tues, Jul 24 2012 9:02 am
Subject: 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »