私のDB構造:
CREATE TABLE IF NOT EXISTS `pos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`alias` varchar(255) NOT NULL,
`model` varchar(255) NOT NULL,
`foreign_key` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `alias` (`alias`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `psos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
`po_id` int(11) NOT NULL,
`lft` int(11) NOT NULL DEFAULT '0',
`rght` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
私のモデルの構造:
class Po extends Model {
public $name = 'Po';
public $useTable = 'pos';
public $cacheQueries = false;
public $hasMany = array(
'Pso' => array(
'className' => 'Pso',
'foreignKey' => 'po_id',
'dependent' => true,
),
'Puso' => array(
'className' => 'Puso',
'foreignKey' => 'po_id',
'dependent' => true,
),
);
}
class Pso extends Model {
public $name = 'Pso';
public $useTable = 'psos';
public $cacheQueries = false;
public $actsAs = array(
'Tree',
);
public $belongsTo = array(
'Parent' => array(
'className' => 'Pso',
'foreignKey' => 'parent_id',
),
'Po' => array(
'className' => 'Po',
'foreignKey' => 'po_id',
),
);
public $hasMany = array(
'Children' => array(
'className' => 'Pso',
'foreignKey' => 'parent_id',
),
);
}
My Saving Associated テスト コード:
$Po->saveAssociated(array(
'Po' => array(
'alias' => 'teste'.uniqid(),
),
'Pso' => array(
'parent_id' => 2,
),
));
結果として PSO が保存された行:
array('id'=>4,'parent_id'=>0,'po_id'=>4,'lft'=>7,'rght'=>8)
予想される PSO の保存行:
array('id'=>4,'parent_id'=>2,'po_id'=>4,'lft'=>7,'rght'=>8)