Re: Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-03 Thread Jeremy Burns
Have you considered an approach where you save A and B independently
(i.e. without worrying about the connecting ids), collecting the ids
of both A and B (using $this-A-id immediately after save) and then
doing a subsequent update?

On Dec 3, 7:54 am, Quess miroslaw.niepo...@gmail.com wrote:
 @Miles J
 Your idea won't work with mysql and id field as autoincrement (dunno
 how with other db but i assume that can be the same).
 If you have 100 rows with id from 1 to 100 and you will delete last 5
 (from 96 to 100), then doing insert new rown you will get id 101, not
 96.

 @DigitalDude
 Please take a look athttp://blog.jamiedoris.com/geek/560/

 Best Regards,
 Miro

 On Dec 3, 12:40 am, Miles J mileswjohn...@gmail.com wrote:



  Its impossible to get the ID for a row your about to save, until after
  you save it.

  The most you can do is do a query to select the last row in the table,
  then increment its ID by one.

  On Dec 2, 2:18 pm, Dave davidcr...@gmail.com wrote:

   I think this would really help your situation

  http://bakery.cakephp.org/articles/view/wizard-component-1-2-1

   I have used it in the past and if you read the documentation thoroughly it
   is very easy to implement.

   On Wed, Dec 2, 2009 at 1:05 PM, DigitalDude 
   e.blumsten...@googlemail.comwrote:

Hey,

I have a serious problem, and I need a hint from you guys to solve it!

In one of my controller actions, I have the problem that I need to
create some records , let's say A, B, and C.

The problem is, that record A needs a field from record B, which is
the ID of record B, and which I only can set when record A is saved,
and B is saved. In model C I need some information from the new
created models A and B, which I only have when model A and B are
saved.

As this has to do with a register-process for my app, I need to
validate the data for all models. The problem is, that when model A
passes validation, and get's saved (which is needed to save the other
models) and model B fails, I have a never used record of model A in
my database. I need to get around that, because that's like a worst
case for me!

I hope there's any chance to create a record, set this record with the
data I get from my registration form, and use the data for the other
records and save everything at the end of the action when every record
validates!

I know I can check a record for validation by setting the model data
with

$this-Model-set($this-data)

and then

if($this-Model-validates()) {
   save the record(s)
}

But right now I'm only able to do this when I have an edit action and
I set the Model to a specific record with like

$this-Model-id = $id_from_get_parameter_or_so

It would be VERY cool if there is a slight chance that I could create
a new record with current auto-increment id from the database, use
that id for my other models, and at the end when I created all needed
records I'd check every record for validation and if every record is
ok, they all get saved.

If anyone knows how to achieve this, I would be very very happy!

Cheers,

DD

P.S.:

If needed, I can post the code of the whole RequestAction to see if
maybe there's another solution to this...

Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpothers
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.comcake-php%2bunsubscr...@googlegroups.c
 omFor more options, visit this group at
   http://groups.google.com/group/cake-php?hl=en

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


Re: Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-03 Thread Jeremy Burns
I forgot to add...

If the insert of B fails, you can delete A because you know the id of
the most recently added row in A.

On Dec 3, 8:32 am, Jeremy Burns jeremybu...@me.com wrote:
 Have you considered an approach where you save A and B independently
 (i.e. without worrying about the connecting ids), collecting the ids
 of both A and B (using $this-A-id immediately after save) and then
 doing a subsequent update?

 On Dec 3, 7:54 am, Quess miroslaw.niepo...@gmail.com wrote:



  @Miles J
  Your idea won't work with mysql and id field as autoincrement (dunno
  how with other db but i assume that can be the same).
  If you have 100 rows with id from 1 to 100 and you will delete last 5
  (from 96 to 100), then doing insert new rown you will get id 101, not
  96.

  @DigitalDude
  Please take a look athttp://blog.jamiedoris.com/geek/560/

  Best Regards,
  Miro

  On Dec 3, 12:40 am, Miles J mileswjohn...@gmail.com wrote:

   Its impossible to get the ID for a row your about to save, until after
   you save it.

   The most you can do is do a query to select the last row in the table,
   then increment its ID by one.

   On Dec 2, 2:18 pm, Dave davidcr...@gmail.com wrote:

I think this would really help your situation

   http://bakery.cakephp.org/articles/view/wizard-component-1-2-1

I have used it in the past and if you read the documentation thoroughly 
it
is very easy to implement.

On Wed, Dec 2, 2009 at 1:05 PM, DigitalDude 
e.blumsten...@googlemail.comwrote:

 Hey,

 I have a serious problem, and I need a hint from you guys to solve it!

 In one of my controller actions, I have the problem that I need to
 create some records , let's say A, B, and C.

 The problem is, that record A needs a field from record B, which is
 the ID of record B, and which I only can set when record A is saved,
 and B is saved. In model C I need some information from the new
 created models A and B, which I only have when model A and B are
 saved.

 As this has to do with a register-process for my app, I need to
 validate the data for all models. The problem is, that when model A
 passes validation, and get's saved (which is needed to save the other
 models) and model B fails, I have a never used record of model A in
 my database. I need to get around that, because that's like a worst
 case for me!

 I hope there's any chance to create a record, set this record with the
 data I get from my registration form, and use the data for the other
 records and save everything at the end of the action when every record
 validates!

 I know I can check a record for validation by setting the model data
 with

 $this-Model-set($this-data)

 and then

 if($this-Model-validates()) {
    save the record(s)
 }

 But right now I'm only able to do this when I have an edit action and
 I set the Model to a specific record with like

 $this-Model-id = $id_from_get_parameter_or_so

 It would be VERY cool if there is a slight chance that I could create
 a new record with current auto-increment id from the database, use
 that id for my other models, and at the end when I created all needed
 records I'd check every record for validation and if every record is
 ok, they all get saved.

 If anyone knows how to achieve this, I would be very very happy!

 Cheers,

 DD

 P.S.:

 If needed, I can post the code of the whole RequestAction to see if
 maybe there's another solution to this...

 Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpothers
 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.comcake-php%2bunsubscr...@googlegroups.c
  omFor more options, visit this group at
http://groups.google.com/group/cake-php?hl=en

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


Re: Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-03 Thread DigitalDude
Hey,

@Jeremy:

That's what I did after  few tries.

I set the fields where I need foreign keys to static values, checked
the user-input of every model with $this-model-validates() and if
sth. is wrong, I invalidate the field so I can put error messages
besides the normal validation ones wherever I want to. When all user-
given data is validatet, I save the models and use the id's to set the
fields of the related models.

It wasn't so bad at all, but when you use invalidate() you have to
consider some REALLY important things, such as no use of READ in the
action, as this will break the displaying of the field-related error
messages (believe me, it does, and it took me quite a bit to figure
that out!)

Also you need to save the output of $this-model-invalidFields()
because when you don't, error displaying is broken again. That's a
known issue, hope they'll fix it in Cake 1.3.

This was a tricky one, but in the end I'm really happy it worked out!

:)

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


Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-02 Thread DigitalDude
Hey,

I have a serious problem, and I need a hint from you guys to solve it!

In one of my controller actions, I have the problem that I need to
create some records , let's say A, B, and C.

The problem is, that record A needs a field from record B, which is
the ID of record B, and which I only can set when record A is saved,
and B is saved. In model C I need some information from the new
created models A and B, which I only have when model A and B are
saved.

As this has to do with a register-process for my app, I need to
validate the data for all models. The problem is, that when model A
passes validation, and get's saved (which is needed to save the other
models) and model B fails, I have a never used record of model A in
my database. I need to get around that, because that's like a worst
case for me!

I hope there's any chance to create a record, set this record with the
data I get from my registration form, and use the data for the other
records and save everything at the end of the action when every record
validates!

I know I can check a record for validation by setting the model data
with

$this-Model-set($this-data)

and then

if($this-Model-validates()) {
save the record(s)
}

But right now I'm only able to do this when I have an edit action and
I set the Model to a specific record with like

$this-Model-id = $id_from_get_parameter_or_so


It would be VERY cool if there is a slight chance that I could create
a new record with current auto-increment id from the database, use
that id for my other models, and at the end when I created all needed
records I'd check every record for validation and if every record is
ok, they all get saved.

If anyone knows how to achieve this, I would be very very happy!

Cheers,

DD

P.S.:

If needed, I can post the code of the whole RequestAction to see if
maybe there's another solution to this...

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


Re: Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-02 Thread Dave
I think this would really help your situation

http://bakery.cakephp.org/articles/view/wizard-component-1-2-1

I have used it in the past and if you read the documentation thoroughly it
is very easy to implement.

On Wed, Dec 2, 2009 at 1:05 PM, DigitalDude e.blumsten...@googlemail.comwrote:

 Hey,

 I have a serious problem, and I need a hint from you guys to solve it!

 In one of my controller actions, I have the problem that I need to
 create some records , let's say A, B, and C.

 The problem is, that record A needs a field from record B, which is
 the ID of record B, and which I only can set when record A is saved,
 and B is saved. In model C I need some information from the new
 created models A and B, which I only have when model A and B are
 saved.

 As this has to do with a register-process for my app, I need to
 validate the data for all models. The problem is, that when model A
 passes validation, and get's saved (which is needed to save the other
 models) and model B fails, I have a never used record of model A in
 my database. I need to get around that, because that's like a worst
 case for me!

 I hope there's any chance to create a record, set this record with the
 data I get from my registration form, and use the data for the other
 records and save everything at the end of the action when every record
 validates!

 I know I can check a record for validation by setting the model data
 with

 $this-Model-set($this-data)

 and then

 if($this-Model-validates()) {
save the record(s)
 }

 But right now I'm only able to do this when I have an edit action and
 I set the Model to a specific record with like

 $this-Model-id = $id_from_get_parameter_or_so


 It would be VERY cool if there is a slight chance that I could create
 a new record with current auto-increment id from the database, use
 that id for my other models, and at the end when I created all needed
 records I'd check every record for validation and if every record is
 ok, they all get saved.

 If anyone knows how to achieve this, I would be very very happy!

 Cheers,

 DD

 P.S.:

 If needed, I can post the code of the whole RequestAction to see if
 maybe there's another solution to this...

 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.comcake-php%2bunsubscr...@googlegroups.comFor
  more options, visit this group at
 http://groups.google.com/group/cake-php?hl=en


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


Re: Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-02 Thread Miles J
Its impossible to get the ID for a row your about to save, until after
you save it.

The most you can do is do a query to select the last row in the table,
then increment its ID by one.

On Dec 2, 2:18 pm, Dave davidcr...@gmail.com wrote:
 I think this would really help your situation

 http://bakery.cakephp.org/articles/view/wizard-component-1-2-1

 I have used it in the past and if you read the documentation thoroughly it
 is very easy to implement.

 On Wed, Dec 2, 2009 at 1:05 PM, DigitalDude 
 e.blumsten...@googlemail.comwrote:

  Hey,

  I have a serious problem, and I need a hint from you guys to solve it!

  In one of my controller actions, I have the problem that I need to
  create some records , let's say A, B, and C.

  The problem is, that record A needs a field from record B, which is
  the ID of record B, and which I only can set when record A is saved,
  and B is saved. In model C I need some information from the new
  created models A and B, which I only have when model A and B are
  saved.

  As this has to do with a register-process for my app, I need to
  validate the data for all models. The problem is, that when model A
  passes validation, and get's saved (which is needed to save the other
  models) and model B fails, I have a never used record of model A in
  my database. I need to get around that, because that's like a worst
  case for me!

  I hope there's any chance to create a record, set this record with the
  data I get from my registration form, and use the data for the other
  records and save everything at the end of the action when every record
  validates!

  I know I can check a record for validation by setting the model data
  with

  $this-Model-set($this-data)

  and then

  if($this-Model-validates()) {
     save the record(s)
  }

  But right now I'm only able to do this when I have an edit action and
  I set the Model to a specific record with like

  $this-Model-id = $id_from_get_parameter_or_so

  It would be VERY cool if there is a slight chance that I could create
  a new record with current auto-increment id from the database, use
  that id for my other models, and at the end when I created all needed
  records I'd check every record for validation and if every record is
  ok, they all get saved.

  If anyone knows how to achieve this, I would be very very happy!

  Cheers,

  DD

  P.S.:

  If needed, I can post the code of the whole RequestAction to see if
  maybe there's another solution to this...

  Check out the new CakePHP Questions sitehttp://cakeqs.organd 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.comcake-php%2bunsubscr...@googlegroups.comFor
   more options, visit this group at
 http://groups.google.com/group/cake-php?hl=en

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


Re: Any chance to get the ID of a new created record BEFORE I save it to the DB?

2009-12-02 Thread Quess
@Miles J
Your idea won't work with mysql and id field as autoincrement (dunno
how with other db but i assume that can be the same).
If you have 100 rows with id from 1 to 100 and you will delete last 5
(from 96 to 100), then doing insert new rown you will get id 101, not
96.

@DigitalDude
Please take a look at http://blog.jamiedoris.com/geek/560/

Best Regards,
Miro

On Dec 3, 12:40 am, Miles J mileswjohn...@gmail.com wrote:
 Its impossible to get the ID for a row your about to save, until after
 you save it.

 The most you can do is do a query to select the last row in the table,
 then increment its ID by one.

 On Dec 2, 2:18 pm, Dave davidcr...@gmail.com wrote:

  I think this would really help your situation

 http://bakery.cakephp.org/articles/view/wizard-component-1-2-1

  I have used it in the past and if you read the documentation thoroughly it
  is very easy to implement.

  On Wed, Dec 2, 2009 at 1:05 PM, DigitalDude 
  e.blumsten...@googlemail.comwrote:

   Hey,

   I have a serious problem, and I need a hint from you guys to solve it!

   In one of my controller actions, I have the problem that I need to
   create some records , let's say A, B, and C.

   The problem is, that record A needs a field from record B, which is
   the ID of record B, and which I only can set when record A is saved,
   and B is saved. In model C I need some information from the new
   created models A and B, which I only have when model A and B are
   saved.

   As this has to do with a register-process for my app, I need to
   validate the data for all models. The problem is, that when model A
   passes validation, and get's saved (which is needed to save the other
   models) and model B fails, I have a never used record of model A in
   my database. I need to get around that, because that's like a worst
   case for me!

   I hope there's any chance to create a record, set this record with the
   data I get from my registration form, and use the data for the other
   records and save everything at the end of the action when every record
   validates!

   I know I can check a record for validation by setting the model data
   with

   $this-Model-set($this-data)

   and then

   if($this-Model-validates()) {
      save the record(s)
   }

   But right now I'm only able to do this when I have an edit action and
   I set the Model to a specific record with like

   $this-Model-id = $id_from_get_parameter_or_so

   It would be VERY cool if there is a slight chance that I could create
   a new record with current auto-increment id from the database, use
   that id for my other models, and at the end when I created all needed
   records I'd check every record for validation and if every record is
   ok, they all get saved.

   If anyone knows how to achieve this, I would be very very happy!

   Cheers,

   DD

   P.S.:

   If needed, I can post the code of the whole RequestAction to see if
   maybe there's another solution to this...

   Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp 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.comcake-php%2bunsubscr...@googlegroups.comFor
more options, visit this group at
  http://groups.google.com/group/cake-php?hl=en

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