Skip to content

Instantly share code, notes, and snippets.

@zoghal
Last active December 17, 2015 01:09
Show Gist options
  • Save zoghal/5526172 to your computer and use it in GitHub Desktop.
Save zoghal/5526172 to your computer and use it in GitHub Desktop.
CakePHP - virtual tree
<?php
class Page extends AppModel{
public $useTable = 'objects';
public $actsAs = array(
'Tree' => array( 'recursive' => 0 )
);
public $virtualFields = array(
'lft' => 'Tree.lft',
'rght' => 'Tree.rght',
'parent_id' => 'Tree.parent_id',
)
public $hasOne = array(
'Tree' => array(
'className' => 'Tree',
'foreignKey' => 'object_id',
'dependent' => true
),
);
}
CREATE TABLE `objects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`site_id` int(11) DEFAULT NULL,
`type` varchar(255) COLLATE utf8_persian_ci NOT NULL,
`model` varchar(255) COLLATE utf8_persian_ci NOT NULL,
`lang` varchar(5) COLLATE utf8_persian_ci NOT NULL,
`name` varchar(255) COLLATE utf8_persian_ci NOT NULL,
`alias` varchar(255) COLLATE utf8_persian_ci NOT NULL,
`home` tinyint(1) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '1',
`content_count` int(11) DEFAULT '0',
`service_count` int(11) DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modifed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `fk_objects_sites_1` (`site_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
CREATE TABLE `objects_tree` (
`object_id` int(11) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
`lft` int(11) NOT NULL,
`rght` int(11) DEFAULT NULL,
PRIMARY KEY (`object_id`),
KEY `fk_objects_sites_1` (`object_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
<?php
class Tree extends AppModel{
public $useTable = 'objects_tree';
public $belongsTo = array(
'belongsTo' => array(
'className' => 'Page',
'foreignKey' => 'object_id',
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment