MySQL table:
CREATE TABLE `menus` ( `id` int(11) NOT NULL AUTO_INCREMENT, `label` varchar(100) NOT NULL, `icon` varchar(50) NOT NULL, `node` tinyint(1) NOT NULL DEFAULT '0', `controller` varchar(50) DEFAULT NULL, `action` varchar(50) DEFAULT NULL, `tab` varchar(50) DEFAULT NULL, `weight` int(2) NOT NULL DEFAULT '10', `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, `menu_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_MENU_MENU` (`menu_id`), CONSTRAINT `FK_MENU_MENU` FOREIGN KEY (`menu_id`) REFERENCES `menus` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=164 DEFAULT CHARSET=utf8;
table initialize:
public function initialize(array $config) { parent::initialize($config); $this->setTable('menus'); $this->setDisplayField('label'); $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); $this->belongsTo('MenuPai', [ 'strategy' => 'select', 'className' => 'Menus', 'propertyName' => 'menu_pai', 'foreignKey' => 'menu_id', ]); $this->hasMany('MenusFilhos', [ 'strategy' => 'select', 'className' => 'Menus', 'propertyName' => 'menus_filhos', 'foreignKey' => 'menu_id', ]); }
Find:
$this->loadModel(‘Menus’)->find()->where([‘Menus.menu_id IS NULL’])->toArray();
my beforeFind:
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { $query->contain(['MenusFilhos']); $query->order([$query->getRepository()->getAlias() . '.weight' => 'ASC', $query->getRepository()->getAlias() . '.id' => 'ASC']); }
When I select only one level (child) it sets up the perfect Array, but if I call more levels the Array returns blank …
registes exemples:
insert into
menus
(id
,label
,icon
,node
,controller
,action
,tab
,weight
,created
,modified
,menu_id
)
values
(1,‘Cadastro’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:54:23’,‘2020-08-17 17:54:23’,NULL),
(2,‘DSA’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:54:37’,‘2020-08-17 17:54:37’,NULL),
(3,‘Mala Direta’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:54:57’,‘2020-08-17 17:54:57’,NULL),
(4,‘Timesheet’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:56:43’,‘2020-08-17 17:56:43’,NULL),
(5,‘Gerir Jurídico’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:57:19’,‘2020-08-17 17:57:19’,NULL),
(6,‘Jurídico’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:58:55’,‘2020-08-17 17:58:55’,NULL),
(7,‘Atendimento’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:59:21’,‘2020-08-17 17:59:21’,NULL),
(8,‘Financeiro’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 17:59:35’,‘2020-08-17 17:59:35’,NULL),
(9,‘Administrativo’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 18:08:42’,‘2020-08-17 18:08:42’,1),
(10,‘Tributos/Impostos’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 18:20:00’,‘2020-08-17 18:20:00’,1),
(11,‘Faturamento’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-17 18:20:37’,‘2020-08-17 18:20:37’,1),
(12,‘Financeiro’,‘fa-circle-o’,1,‘’,‘’,NULL,10,‘2020-08-20 17:33:14’,‘2020-08-20 17:33:14’,1),
(13,‘Empresa’,‘fa-circle-o’,0,‘CadastroFiscais’,‘index’,NULL,10,‘2020-08-20 17:35:19’,‘2020-08-20 17:35:19’,9),
(14,‘Cargos’,‘fa-circle-o’,0,‘Cargos’,‘index’,NULL,10,‘2020-08-20 17:37:05’,‘2020-08-20 17:37:05’,9);