Wednesday, October 20, 2010

Re: Find condition on joined table

The relationship has to be defined in both models. So make sure you
define Customers as belonging to Users and Orders belonging to
customers.
http://book.cakephp.org/view/1039/Associations-Linking-Models-Together#belongsTo-1042

On Oct 18, 8:02 am, Dan <dan.nichol...@gmail.com> wrote:
> Hi,
>
> I am trying out CakePHP on an existing (and messy) website to see if
> it is going to be beneficial to use it for the new features from now
> on. I have been creating a test model that allows admin to search
> through users based on various fields (userId, name, email address,
> orderId).
>
> I have 3 tables that are being used for this:
> Users
> Customers
> Orders
>
> Users links to Customers on a 1 to 1 relationship on 'userId',
> Customers links to Orders on a 1 to many relationship on 'customerId'.
>
> The search feature is working correctly, except that I can't figure
> out how to set conditions on the Orders table.
> This is the query I would write normally to get the user who placed a
> certain order:
>
> SELECT * FROM users u
> LEFT JOIN customers c ON u.userId = c.userId
> LEFT JOIN orders o ON o.customerId = c.customerId
> WHERE o.orderId = $orderId.
>
> I have set up models for Users, Customers and Orders, With 'Users
> $hasOne ' referencing Customers, and 'Customers $hasMany' referencing
> Orders.
>
> If the conditions only reference the Users table, the script brings
> out the Orders data correctly, but I can't get it to allow me to
> search for an orderId, as Orders is referenced by Users through
> Customers.
>
> Any help on this would be great, I can upload code if necessary.

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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate