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 cake-php@googlegroups.com
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?hl=en
No comments:
Post a Comment