As far as I can understand from your tables, and your own statement:

> Tuotteet hasOne Tekstit which hasMany Kaannokset.

then the following associations should be defined:
1) Tuotteet hasOne Tekstit
This means that Tekstit has a field named tuotteet_id if you follow
the CakePHP conventions.
But looking at your class Tuotteet, I assume that you want the id of
both tables to be equal, so your hasOne association should be defined
as:
[code]
   var $hasOne = array(
       'Tekstit' => array(
           'className' => 'Tekstit',
           'foreignKey' => false,
           'conditions' => 'Tekstit.id = Tuotteet.id'
       )
   );
[/code]

2) Tekstit hasMany Kaannokset
This means that Kaannokset has a field name tekstit_id, thus your
class Kaannokset should define the belongsTo association as:
[code]
   var $belongsTo = array(
       'Tekstit' => array(
           'className' => 'Tekstit',
           'foreignKey' => 'tekstit_id',
       )
   );
[/code]

Hope this helps you on the way,
   John

On Feb 2, 6:37 pm, doze <[email protected]> wrote:
> Hello!
>
> I have following database tables and relations:
>
> |---------------|               |-------------|
> |--------------------|
> | Tuotteet  |               | Tekstit  |                 | Kaannokset
> |
> |---------------|               |-------------|
> |--------------------|
> | teksti_id  | hasOne  | id          |  hasMany | teksti_id      |
> | ....          |               | ....        |
> | ....               |
> | ....          |               | ....        |
> | ....               |
> |---------------|               |-------------|
> |--------------------|
>
> Eg.
>
> Tuotteet hasOne Tekstit which hasMany Kaannokset.
>
> Here's the models:
>
> <?php
> class Tuotteet extends AppModel {
>     var $name = 'Tuotteet';
>     var $useTable = 'tuotteet';
>     var $hasOne = array(
>         'Tekstit' => array(
>             'className' => 'Tekstit',
>             'foreignKey' => false,
>             'conditions' => 'Tekstit.id = Tuotteet.teksti_id'
>         )
>     );}
>
> ?>
>
> <?php
> class Tekstit extends AppModel {
>     var $name = 'Tekstit';
>     var $useTable = 'tekstit';
>     var $hasMany = array(
>         'Kaannokset' => array(
>             'className' => 'Kaannokset',
>             'foreignKey' => 'teksti_id',
>             'dependent' => true
>         )
>     );}
>
> ?>
>
> <?php
> class Kaannokset extends AppModel {
>     var $name = 'Kaannokset';
>     var $useTable = 'kaannokset';
>     var $belongsTo = array(
>         'Tekstit' => array(
>             'className' => 'Tekstit',
>             'foreignKey' => 'id',
>         )
>     );}
>
> ?>
>
> And the current situation and problem...
>
> I'm trying to query all "Tuotteet" and get all "Kaannokset" for it.
>
> Here's the controller code:
>
> <?php
> class TuotteetController extends AppController {
>
>     var $name = 'Tuotteet';
>
>     function index() {
>         $this->Tuotteet->recursive = 1;
>         $this->set('tuotteet', $this->paginate());
>     }
>
> }
>
> ?>
>
> If $this->Tuotteet->recursive is set to 1, everything works and it
> returns:
>
> Array
> (
>     [Tuotteet] => Array
>         (
>             [id] => 1
>             [tuotenro] => 133
>             [teksti_id] => 2
>             [kuva] => /img/data/tuotteet/Water lilies.jpg
>         )
>
>     [Tekstit] => Array
>         (
>             [id] => 2
>             [teksti] => Kuvasarja 5 x 5 x 5
>         )
> )
>
> But to get also all "Kaannokset" for that "Tekstit" record, I set
> $this->Tuotteet->recursive to 2.
>
> That leads to following errors:
>
> Warning (2): Invalid argument supplied for foreach() [CORE/cake/libs/
> model/datasources/dbo_source.php, line 1229]
>
> Warning (512): SQL Error: 1054: Unknown column 'Tuotteet.teksti_id' in
> 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line
> 635]
>
> Query: SELECT `Tekstit`.`id`, `Tekstit`.`teksti` FROM `tekstit` AS
> `Tekstit` WHERE `Tekstit`.`id` = `Tuotteet`.`teksti_id`
>
> Why?
>
> And how should this be done??

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