I read
http://groups.google.com/group/akelos-framework/browse_thread/thread/1d2e06ebf2437529/481ab6ed204971ae?lnk=gst&q=order#481ab6ed204971ae
but haven't found the answer to the question.
----
class Owner extends ActiveRecord {
}
----
//This code was generated automatically by the active record
hasAndBelongsToMany Method
class Ownership extends ActiveRecord {
var $belongs_to = array('owner', 'piece');
var $_avoidTableNameValidation = true;
function Ownership()
{
$this->setModelName("Ownership");
$attributes = (array)func_get_args();
$this->setTableName('ownerships', true, true);
$this->init($attributes);
}
}
----
class Piece extends ActiveRecord {
var $habtm = array(
'owners' => array(
'join_class_name' => 'Ownership',
'join_table' => 'ownerships',
'order' => '_Ownership.date_bought DESC'
)
);
}
----
How can I for example access the "date_bought" property
from table "ownerships"?
I have $this->piece->owners (array of Owner objects) in the piece
controller,
but where do I find "Ownership"?
tia
bye
Wolfgang
>
> *hiya!*
[...]
> class Piece extends ActiveRecord {
> var $habtm = array(
> 'owners' => array(
> 'join_class_name' => 'Ownership',
> 'join_table' => 'ownerships',
> 'order' => '_Ownership.date_bought DESC'
> )
> );
> }
> ----
since we dont have "through"-associations yet, we have to do it by hand.
first declare
Piece hasMany Ownerships.
now its best to define a new method in your piece-Model
function &findWithOwnerships($fetch='all',$options){
$Pieces =&
$this->find($fetch,array_merge(array('include'=>array('ownerships')),
$options));
//now we have to load the Owner for each Ownership
foreach (array_keys($Pieces) as $k){
foreach (array_keys($Pieces[$k]->ownerships) as $i){
$Pieces[$k]->ownerships[$i]->owner->load(); // <--
}
}
return $Pieces;
}
now you have a Pieces-collection
each Piece hasMany Ownerships
and each Ownership belongsTo (one) owner
$Ownership->date_bought;
$Ownership->owner->name;
*not tested*
regards
Kaste
On Oct 29, 7:50 pm, Kaste <thd...@gmx.net> wrote:
> now you have a Pieces-collection
> each Piece hasMany Ownerships
> and each Ownership belongsTo (one) owner
>
> $Ownership->date_bought;
> $Ownership->owner->name;
thanks for the valuable feedback... I solved it like this:
class Piece extends ActiveRecord {
var $has_many = array('ownerships');
}
...
class Ownership extends ActiveRecord {
var $belongs_to = array('owner', 'piece');
}
...
class PieceController extends ApplicationController {
function overview() {
$this->piece =& $this->Piece->find(@$this->params['id'],
array('include' => array('ownership')));
foreach ($this->piece->ownerships as &$ownership) {
$ownership->owner->load();
// additional owner associations
$ownership->owner->country->load();
$ownership->owner->note->load();
}
}
}
hth someone else too, bye
Wolfgang