Yes, probably I'll do as you propose (split to 2x hasMany and 2x
belongsTo), but I suggested that is possible to validate HABTM data,
because saveAll() handles perfect with additional data in join table.

So why I can easly save data, but can't validate? What about 'with'
parameter in hasAndBelongsToMany relation?



On Dec 9, 2:45 pm, euromark <dereurom...@googlemail.com> wrote:
> cake HABTM does only work with 3 fields
> id
> foreign_id1
> foreign_id2
>
> nothing more or less
>
> if you need to add created, modified, amount, ...
> you will have to split it into 2 "hasMany" + 2 "belongsTo" relations
>
> this is always possible - but maybe not as easy to handle anymore
> it has some advantages, though, too: it doesnt delete and insert all
> records after each save()
>
> the cake habtm might be ok for some relations, but
> i usually always use those split tables as they are way more powerful!
>
> On 9 Dez., 14:28, red <mbu...@gmail.com> wrote:
>
>
>
> > Hello, I've already asked on cakeqs.org but I'll try also here.
>
> > My case is: Order HABTM Product. In joining table orders_products I've
> > extra field - quantity. Now, when editing Order I can add Products and
> > enter quantitiy. Here is what I've done already:
>
> > In model Order:
> > public $hasAndBelongsToMany = array(
> >     'Product' => array(
> >         'className' => 'Product',
> >         'joinTable' => 'orders_products',
> >         'foreignKey' => 'order_id',
> >         'associationForeignKey' => 'product_id',
> >         'unique' => true,
> >         'with' => 'OrdersProduct'
> >     )
> > );
>
> > In model Product:
> > public $hasAndBelongsToMany = array(
> >     'Order' => array(
> >         'className' => 'Order',
> >         'joinTable' => 'orders_products',
> >         'foreignKey' => 'product_id',
> >         'associationForeignKey' => 'order_id',
> >         'unique' => true,
> >         'with' => 'OrdersProduct'
> >     )
> > );
>
> > I've created model for joining table OrdersProduct:
> > class OrdersProduct extends AppModel {
> >     public $name = 'OrdersProduct';
>
> >     public $validate = array(
> >         'quantity' => array(
> >             'numeric' => array('rule' => array('notempty'))
> >         )
> >     );
>
> > }
>
> > In orders editing view I've:
> > <?php echo $this->Form->input("Product.0.OrdersProduct.product_id",
> > array('label' => false, 'options' => $products)); ?>
> > <?php echo $this->Form->input("Product.0.OrdersProduct.quantity", array
> > ('label' => false, 'class' => 'text')); ?>
>
> > Now in Orders controller try to save:
> >  // Form processing
> >     if (!empty($this->data)) {
> >         $this->Order->OrdersProduct->set($this->data);
> >         debug($this->Order->OrdersProduct->validates());
>
> >         if ($this->Order->saveAll($this->data)) {
> >             // Do some stuff
> >         }
> >     }
>
> > And $this->Order->OrdersProduct->validates() always returns TRUE, no
> > matter if I enter quantity or not. All the data saves perfect, only no
> > validation is problem. What I'm missing?

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