Your model relationship should actually be:

Item belongsTo Unit
Unit hasMany Items (optional)

In the Cookbook subsection ""3.7.6.3 belongsTo" (http://
book.cakephp.org/view/1039/Associations-Linking-Models-
Together#belongsTo-1042, part of "3.7.6 Associations: Linking Models
Together"), there is a note:

"If a model(table) contains a foreign key, it belongsTo the other
model(table)."




On May 29, 3:53 pm, Mondo <[email protected]> wrote:
> I have Items and Units, and want each Item to have one Unit, but that
> unit can be used by many.
>
> Items Table
> id - int
> name - varchar
> description varchar
> unit_id - int
>
> Units Table
> id - int
> name - varchar
> description - varchar
>
> Items Model
> var $hasOne = array( 'Units' => array( 'className' => 'Unit' ) );
>
> Units Model
> Nothing, but previously I tried:  var $belongsTo = array( 'Items' =>
> array( 'className' => 'Item' ) );
>
> When I'm in the scaffolding (List Items) I get:
> Warning (512): SQL Error: 1054: Unknown column 'Units.item_id' in 'on
> clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
>
> And in the Edit Item screen, the Units drop-down is empty. Any idea
> what I'm doing wrong? Essentially the Units table are just to populate
> a drop-down in the Item entry form, but I'd like it to auto-populate
> in scaffold.

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to