I'll give that a try and see how it works out - it would be nice to
figure out what exactly about the manipulation during the model's
beforeSave callback is causing the issue so that I could understand
the process better, but until I've worked with Cake for a little while
longer I think it'll be easier just to get things working first.

On Jun 5, 2:39 pm, vekija <vedran.konto...@gmail.com> wrote:
> Had a similar issue recently when I was trying to save related data
> array with associative indexes.
> Since saveAll() won't work with such indexes I had to convert those to
> the numeric so I put the code in beforeSave() callback.
> It didn't work. But when I put the code in the controller just before
> the saveAll() call everything worked just fine.
> In my case it was a single line of code so I thought it wouldn't hurt
> to much if it was there, so I didn't investigated further.
>
> On Jun 5, 10:24 pm, mwaterous <m...@watero.us> wrote:
>
> > Vekija's method works where mine seems to fail horribly. This has
> > become more of an exploration in Cake than a necessity now, but I'm
> > trying to understand why this particular saveAll() is failing; using
> > the hidden input in combination with the regular input I can
> > effectively save as many rows as I would like to from any given form.
> > However when I try to compensate for the possibility of an associative
> > index appearing in $this->data['MetaUser'] it's failing to insert the
> > rows every time.
>
> > For example, in my view I have;
>
> > echo $form->input( 'MetaUser.activation', array( 'type' => 'checkbox',
> > 'label' => 'blah blah blah' ) );
>
> > echo $form->input( 'MetaUser.0.meta_key', array( 'type' => 'hidden',
> > 'value' => 'optin' ) );
> > echo $form->input( 'MetaUser.0.meta_value', array( 'type' =>
> > 'checkbox', 'label' => 'I want to receive newsletters and updates via
> > email.' ) );
>
> > ...and for clarity it does work fine if I remove the first input, but
> > nevertheless and I'm not sure why, I wanted to write some code to
> > handle this situation transparently. So I wrote a beforeSave()
> > callback in my MetaUser model that looks for such occurrences and
> > fixes them (and then returns true, so that's not the issue). Here is
> > the array at the start of my beforeSave() method;
>
> > Array
> > (
> >     [MetaUser] => Array
> >         (
> >             [activation] => 1
> >             [0] => Array
> >                 (
> >                     [meta_key] => optin
> >                     [meta_value] => 1
> >                     [user_id] => 12
> >                 )
> >         )
> > )
>
> > And here it is at the end, right before the return statement;
>
> > Array
> > (
> >     [MetaUser] => Array
> >         (
> >             [0] => Array
> >                 (
> >                     [meta_key] => optin
> >                     [meta_value] => 1
> >                     [user_id] => 12
> >                 )
>
> >             [1] => Array
> >                 (
> >                     [meta_key] => activation
> >                     [meta_value] => 1
> >                     [user_id] => 12
> >                 )
>
> >         )
>
> > )
>
> > So the array I'm building *looks* like it should work, but after
> > logging the queries and reviewing everything it seems that it doesn't
> > even try to insert the data. It does the first insert, gets the
> > auto_increment value and commits.
>
> > On Jun 5, 9:32 am, mwaterous <m...@watero.us> wrote:
>
> > > I have the following in my User model;
>
> > >         public $hasMany = array(
> > >                 'MetaUser' => array(
> > >                         'className'  => 'MetaUser',
> > >                         'fields'     => array( 'meta_key', 'meta_value' )
> > >                 )
> > >         );
>
> > > ...should I also define a belongsTo relationship from the MetaUser
> > > model, or is this necessary when it's being manipulated from the Users
> > > controller?
>
> > > On Jun 4, 6:49 pm, calvin <cal...@rottenrecords.com> wrote:
>
> > > > Did you define the relationship between User and MetaUser?
>
> > > > i.e. in the User model:
>
> > > > $hasMany = array('MetaUser' => array());
>
> > > > Because both, your approach and vekija's, should work.
>
> > > > On Jun 4, 9:48 am, mwaterous <m...@watero.us> wrote:
>
> > > > > Well my last question here turned out to be between keyboard and
> > > > > chair, so hopefully this isn't quite as silly a question!
>
> > > > > I am learning Cake and building a custom user authentication system
> > > > > (who isn't?). I am trying to use two separate tables to store data,
> > > > > the User Model table as parent which stores only mission critical data
> > > > > (login, password, email, created, etc), and a MetaUser model table
> > > > > which is built around key value pairs (meta_id, user_id, meta_key,
> > > > > meta_value).
>
> > > > > Upon registration I have two pieces of meta information I would like
> > > > > to store - the activation code and an optin for receiving
> > > > > communication from site admins. I have built the forms as instructed
> > > > > by the Cake Book in order to use saveAll() in my Register action. The
> > > > > problem is that this results in the following array:
>
> > > > > Array
> > > > > (
> > > > >     [MetaUser] => Array
> > > > >         (
> > > > >             [optin] => 1
> > > > >             [activation_code] => 9c1a26272907d4196a7bf39d
> > > > >             [user_id] => 17
> > > > >         )
> > > > > )
>
> > > > > This obviously won't save since there is no optin or activation_code
> > > > > column in the MetaUser table. So I tried creating a beforeSave() under
> > > > > the presumption that if this was a numerical array it might try and
> > > > > add multiple rows on its own;
>
> > > > > public function beforeSave() {
>
> > > > >         if ( isset( $this->data['MetaUser']['user_id'] ) ) {
>
> > > > >                 $userid = $this->data['MetaUser']['user_id'];
>
> > > > >                 $new = array();
> > > > >                 $i = 0;
>
> > > > >                 foreach ( $this->data['MetaUser'] as $k => $v ) {
> > > > >                         $new[$i]['user_id']    = $user_id;
> > > > >                         $new[$i]['meta_key']   = $k;
> > > > >                         $new[$i]['meta_value'] = $v;
> > > > >                         $i++;
> > > > >                 }
>
> > > > >                 $this->data['MetaUser'] = $new;
>
> > > > >         }
>
> > > > >         return true;
>
> > > > > }
>
> > > > > But apparently it doesn't work that way. I'm going to try it next
> > > > > using distinct save()'s for $this->User and $this->MetaUser but am I
> > > > > missing something else that I should be doing instead? I want to
> > > > > create a MetaUser model where I can save each entry (even if multiple
> > > > > are coming from one form) as separate rows based on the key => value
> > > > > pair setup.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to