0
CREATE TABLE IF NOT EXISTS `employees` (
  `employees_id` int(11) NOT NULL AUTO_INCREMENT,
  `employees_firstname` varchar(100) DEFAULT NULL,
  `employees_middlename` varchar(45) DEFAULT NULL,
  `employees_lastname` varchar(100) DEFAULT NULL,
  `employee_gender` enum('M','F') NOT NULL,
  `employees_dob` date DEFAULT NULL,
  `employees_emailaddress` varchar(255) DEFAULT NULL,
  `employees_password` varchar(45) DEFAULT NULL,
  `employees_datejoined` date DEFAULT NULL,
  `employees_code` varchar(45) DEFAULT NULL,
  `employees_status` tinyint(1) DEFAULT '1',
  `employees_mobile` varchar(15) DEFAULT NULL,
  `employees_designation` int(11) DEFAULT NULL,
  `employees_bussiness_account` int(11) DEFAULT NULL,
  `employees_image` varchar(255) DEFAULT NULL,
  `employees_supervisers_id` int(11) DEFAULT NULL,
  `employees_work_location_id` int(11) DEFAULT NULL,
  `employees_date_added` datetime DEFAULT NULL,
  PRIMARY KEY (`employees_id`),
  UNIQUE KEY `employees_code_UNIQUE` (`employees_code`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

--
-- Dumping data for table `employees`
--

INSERT INTO `employees` (`employees_id`, `employees_firstname`, `employees_middlename`, `employees_lastname`, `employee_gender`, `employees_dob`, `employees_emailaddress`, `employees_password`, `employees_datejoined`, `employees_code`, `employees_status`, `employees_mobile`, `employees_designation`, `employees_bussiness_account`, `employees_image`, `employees_supervisers_id`, `employees_work_location_id`, `employees_date_added`) VALUES
(16, 'Aneesh', 'C', 'S', 'M', '2012-11-05', 'aneesh.cs@calpinetech.com', '7106499a88fd79197ddbff61829993c9', '2010-12-07', 'cas927', 1, '9876543210', 1, 1, 'Betson.JPG', 0, 1, '2012-11-27 00:00:00'),
(17, 'Betson', 'T', 'Thomas', 'M', '2012-11-05', 'betson@calpinetech.com', '7106499a88fd79197ddbff61829993c9', '2008-12-06', 'cas450', 1, '9876543210', 6, 1, NULL, 16, 1, '2012-11-27 00:00:00'),
(18, 'test', 'test', 'test', 'M', '2012-11-12', 'test@calpinetech.com', '7106499a88fd79197ddbff61829993c9', '2012-11-06', 'cas999', 1, '9876543210', 1, 1, NULL, 16, 1, '2012-11-27 00:00:00'),
(19, 'asdsa', 'asdsad', 'asdsad', 'M', '2012-12-03', 'test@calpinetech.com', 'd8578edf8458ce06fbc5bb76a58c5ca4', '2012-12-24', 'ca0999', 1, '9876543210', 1, 1, NULL, 17, 1, '2012-12-04 00:00:00');

这是我的桌子。这里employees_supervisers_idemployees_id. 我们如何通过cakephp获取递归数据?

4

3 回答 3

0

要构建一棵树,您需要使用TreeBehavior,并在模型中具有parent_id和/或lft字段rght

lft并且rght用于MPTT数据

在您的情况下,您可以配置行为以使用您的employees_supervisers_id字段

于 2012-12-14T22:25:59.933 回答
0

我认为您正在尝试在同一个表中的记录之间创建父/子关系。如果这是您所要求的,您可以在 Employee 模型中创建这些关系,如下所示:

public $belongsTo = array(
    'Supervisor' => array(
        'className' => 'Employee',
        'foreignKey' => 'employee_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
);

public $hasMany = array(
    'Underlings' => array(
        'className' => 'Employee',
        'foreignKey' => 'employees_supervisers_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
);
于 2012-12-14T12:25:46.450 回答
0

如果您创建模型之间的关系,CakePHP 默认会获取它。

如果你这样做:

$employees = $this->Employee->find('all');

并且您已经创建了模型之间的关系,您将能够通过以下方式访问递归数据:

$employees['Employee']['Supervisor']['employees_firstname'];

我建议您查看有关模型之间链接的文档:http: //book.cakephp.org/2.0/en/models/associations-linking-models-together.html

于 2012-12-14T10:01:44.990 回答