Monday, May 30, 2011

Re: One-way relationship

You need the relationship both ways:

Item hasOne Unit
Unit belongsTo Item

Add a few items and units into the database. Then go back to the item
controller, edit action and make sure you get a list of all the units:

$this->Item->Unit->find('list'));

Hopefully that helps.


On May 29, 11:53 pm, Mondo <mo...@nodegarden.net> 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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: