Hmm.. I think I wasn't able to describe my problem fully.. But anyhow,
I resolved it by querying the related 'Kaannokset' records manually
for each 'Tekstit' in the controller, like so:

    function index() {
        $this->Tuotteet->recursive = 1;
        $tuotteetArray = $this->paginate();
        foreach ($tuotteetArray as &$tuotteet) {
            $this->Tuotteet->Tekstit->Kaannokset->contain();
            $kaannokset = $this->Tuotteet->Tekstit->Kaannokset-
>find('all', array(
                'conditions' => array('Kaannokset.teksti_id' =>
$tuotteet['Tekstit']['id'])
            ));
            $tuotteet['Tekstit']['Kaannokset'] = $kaannokset;
        }
        $this->set('tuotteet', $tuotteetArray);
    }

The main issues I have is that my customer has already defined the
database and the columns or table names cannot be changed. He is
already using other columns than 'id' as the primary keys and doing
the relations between tables sucks in cake when not following the
conventions.

What I would need is to be able to define localKey for the model
relations so that cake would use it as the 'id' field.

I saw a pach for this (http://groups.google.com.pk/group/tickets-
cakephp/browse_thread/thread/65488e266621ad0c)

But that patch was closed with note to use: 'foreignKey' => false and
'conditions' and that's what I have tried to use, but it gives me the
sql errors as cake won't do the necessary JOINS according to
conditions.



On Feb 3, 2:23 pm, John Andersen <[email protected]> wrote:
> 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