Thursday, May 24, 2012

Re: Model Associations

On Thu, May 24, 2012 at 3:14 PM, hill180 <hill180@gmail.com> wrote:
> Yes.
>
> As a disclaimer, I didn't write the original schema.
>
> Account_ID is for the systems account number (primary key)
> Account is for the logical account number (used by the company)
>
> The Account Address table use the Account_ID as the foreign key, but the
> Contact Table uses the Account Number as the foreign key.
>
> SQLDUMP:
>
> SELECT [CONTACT_FULL_NAME] AS [Contact__CONTACT_FULL_NAME],
> [Contact].[CONTACT_FIRST_NAME] AS [Contact__CONTACT_FIRST_NAME] FROM
> [CONTACTS] AS [Contact] WHERE [Contact].[Account_ID] = [Accounts].[Account]
>
>
> error:
>
> Error: SQLSTATE[42000]: [Microsoft][SQL Server Native Client
> 11.0][SQL Server]The multi-part identifier "Accounts.Account" could not be
> bound.
>

Right so.

You can do it this way instead. Remove the conditions array from the
model association and put this in:
'finderQuery' => 'SELECT Contact.name FROM contacts AS Contact LEFT
JOIN accounts AS Account ON Contact.account_id=Account.account WHERE
Account.AccountID = {$__cakeID__$}'

You can edit the SELECT query to return whatever values you want but
leave in the {$__cakeID__$} bit as it is the value of the primary key.

I hope this makes sense.

Mike.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: