Re: How to save data in an associative model without deleting the old related records.

2010-03-12 Thread WebbedIT

> id | shop_id | service_id | price
>
> And when I change the data, I lose the field price

Ahh, so we're not talking about a standard HABTM as the join table has
extra fields.  Surely whenever adding/editing links you will also need
to show the price incase it needs updating.  So why are you not saving
this data at the same time, then it would not get lost, it would get
reinserted along with the two ids?

Paul.

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: How to save data in an associative model without deleting the old related records.

2010-03-11 Thread nurvzy
@WebbedIT
'unique' => false does indeed stop cake from running a deleteAll on
the join table before inserting new records.  The benefit of this is
it makes adding additional 'tags' much easier.  Consider the
following: If all you want to do is add a new link between two models,
if unique is set to true you first have to grab all the currently
linked ids and then append your new id to that list and re-save.  If
unique is set to false, all you'll have to do is pass in the new tag
id and all your previous records are preserved (a constant time
operation rather than an N time).

At a web interface level it doesn't make much sense to set unique to
false because maintaining database integrity will be a bitch. The
benefit of a HABTM relationship in CakePHP is the join table and model
is suppose to be invisible to you as the developer. Turning unique =>
false is pretty much saying "I'll handle the join table integrity
myself cake".  It's now up to the developer to maintain database
integrity.

That's how I like to think of it anyway,
Nick


I create a blog and assign some tags to it.
On Mar 10, 2:02 am, WebbedIT  wrote:
> @Weather
> It is standard practice on a HABTM join table to delete all existing
> records then run a new insert, this is the most efficient way to do it
> as requires fewer SQL calls and PHP loops, plus it ensures there is no
> duplication of selected options which would cause problems at a later
> date when updating selections.
>
> Why is this a problem to you?
>
> @nurvzy
> Does 'unique' => 'false' really stop cake running a deleteAll on the
> join table before inserting the new records?
>
> And would you want to allow duplicate records in your join table?
> What would you do with those duplicate records as there is no standard
> way to use a form to de-select an option if it has been chosen twice?
>
> HTH
>
> Paul.

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: How to save data in an associative model without deleting the old related records.

2010-03-11 Thread Weather Forecaster
@WebbedIT

I need to keep the old records as I am in them to store additional
information.
In tabel ShopsMainService i write down the price of services. and I
get

id | shop_id | service_id | price

And when I change the data, I lose the field price

On Mar 10, 11:02 am, WebbedIT  wrote:
> @Weather
>
> Why is this a problem to you?
>

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: How to save data in an associative model without deleting the old related records.

2010-03-10 Thread WebbedIT
@Weather
It is standard practice on a HABTM join table to delete all existing
records then run a new insert, this is the most efficient way to do it
as requires fewer SQL calls and PHP loops, plus it ensures there is no
duplication of selected options which would cause problems at a later
date when updating selections.

Why is this a problem to you?

@nurvzy
Does 'unique' => 'false' really stop cake running a deleteAll on the
join table before inserting the new records?

And would you want to allow duplicate records in your join table?
What would you do with those duplicate records as there is no standard
way to use a form to de-select an option if it has been chosen twice?

HTH

Paul.

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: How to save data in an associative model without deleting the old related records.

2010-03-09 Thread nurvzy
To get what you want you'll need to set 'unique' => false within your
declaration of HABTM association in your model.

//models/shop.php
var $hasAndBelongsToMany = array(
  'MainService' => array(
'className' => 'MainService',
'unique' => false,
  )
);

Here is the documentation of all the available key options for HABTM
and what they do:
http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

Hope that helps,
Nick

On Mar 9, 6:20 am, Weather Forecaster  wrote:
> How do I make so that Cakephp not delete related records before
> adding, new.
>
> There are two models of Shop and MainService.
>
> Contact them so:
>
>      class Shop extends AppModel {
>
>          var $hasAndBelongsToMany = array(
>                 'MainService' =>
>                      array( 'className'              => 'MainService')
>                  );
>      }
>
> Stored in the form:
>
>      $this->Shop->saveAll($this->data);
>
> What happens to the base:
>
>      SELECT `ShopsMainService`.`main_services_id` FROM
> `shops_main_services` AS `ShopsMainService`     WHERE
> `ShopsMainService`.`carwash_id` = 71
>
>     DELETE `ShopsMainService` FROM `shops_main_services` AS
> `ShopsMainService` WHERE `ShopsMainService`.`carwash_id` = 71 AND
> `ShopsMainService`.`main_services_id` IN (41, 32, 30, 29, 27, 26, 23,
> 17, 13, 11, 7, 6, 4, 2)
>
>    INSERT INTO `Shops_main_services` (`carwash_id`,`main_services_id`)
> VALUES (71,'2'), (71,'4'), (71,'6'), (71,'7'), (71,'11'), (71,'13'),
> (71,'17'), (71,'23'), (71,'26'), (71,'27'), (71,'29'), (71,'30'),
> (71,'32'), (71,'41')
>
> How to disable the removal?
>
> The documentation is written here is  
> http://book.cakephp.org/view/75/Saving-Your-Data
>
> "By default when saving a HasAndBelongsToMany relationship, Cake will
> delete all rows on the join table before saving new ones. For example
> if you have a Club that has 10 Children associated. You then update
> the Club with 2 children. The Club will only have 2 Children, not
> 12."
>
> But how to do to disable the removal unclear.
>
> p.s.
>
> Ideally, it was good so CakePHP removed the missing connection add new
> ones, and existing ones are not touched.
>
> Example:
> The database is the connection with ID: 1,3,5,7
>
> Obtain new: 5,7,9
> It is necessary to: remove entries with the Id 1,3
>                             add 9
>                             and 5.7 remain unchanged.

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