I'm trying to limit an association between 2 models using the conditions key.
It works great in the 'parent' Model, but I can't work out how to get it
working properly in the 'child' Model.

Here's the relevant info from the models:
Product( 'id', 'name' );
ProductLoan( 'id', 'product_number', 'product_numbr_is_id' );

The hasMany association works fine in the Product Model:
var $hasMany = array(
                'ProductLoan' => array(
                        'className' => 'ProductLoan',
                        'foreignKey' => 'product_number',
                        'dependent' => false,
                        'conditions' => array(
                                'ProductLoan.product_number_is_id' => 1
                        )
                );

But I can't work out how to do the same thing in the belongsTo association
in ProductLoan Model:
var $belongsTo = array(
                'Product' => array(
                        'className' => 'Product',
                        'foreignKey' => 'product_number',
                        'conditions' => array(
                                'ProductLoan.product_number_is_id' => 1
                        ),
                        'fields' => '',
                        'order' => ''
                )
        );

If I leave the condition out cake tries to find matching products, which
will cause problems if a product_number is ever the same as an id in use
SELECT *
FROM `products` AS `Product` 
WHERE `Product`.`id` = 1234567890

If I put the condition in to belongsTo cake will try to use it by adding a
where to the sql
SELECT * 
FROM `products` AS `Product` 
WHERE `Product`.`id` = 1234567890 
AND `ProductLoan`.`product_number_is_id` = 1;

What I really need is something like this to be generated
SELECT * 
FROM `products` AS `Product`
INNER JOIN `product_loans` AS `ProductLoan`
ON `Product`.`id` = `ProductLoan`.`product_number`
AND `ProductLoan`.`product_number_is_id` = 1
WHERE `Product`.`id` = 1234567890;

That would ensure that a manually entered product number of 1234567890 would
never accidentally pick up the wrong product, which happened to have an id
of 1234567890.

Is there a way of setting up the association to use a more complicated sql
statement like that?

(NB I have no control over the product numbers used, and I have to allow for
data entry when the product number typed doesn't match a product that
already exists in the database)

Thanks for any insight on this.
Mike
-- 
View this message in context: 
http://old.nabble.com/Special-Cade-for-belongsTo-association-tp28287758p28287758.html
Sent from the CakePHP mailing list archive at Nabble.com.

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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to