不能在CakePHP 3中重新声明Cake \ ORM \ Behavior \类

时间:2015-05-18 04:13:12

标签: cakephp-3.0

我创建了<div>

src/Model/Behavior/ModernTreeBehavior.php

我在CommentTables.php中使用了<?php namespace Cake\ORM\Behavior; use Cake\Datasource\EntityInterface; use Cake\Datasource\Exception\RecordNotFoundException; use Cake\Event\Event; use Cake\ORM\Behavior; use Cake\ORM\Entity; use Cake\ORM\Query; use InvalidArgumentException; use RuntimeException; class ModernTreeBehavior extends Behavior { /** * Cached copy of the first column in a table's primary key. * * @var string */ protected $_primaryKey; /** * Default config * * These are merged with user-provided configuration when the behavior is used. * * @var array */ protected $_defaultConfig = [ 'implementedFinders' => [ 'children' => 'findChildren', 'treeList' => 'findTreeList' ], 'implementedMethods' => [ 'childCount' => 'childCount', 'getLevel' => 'getLevel' ], 'parent' => 'parent_id', 'path' => 'path', 'scope' => null, 'level' => null ]; public function getLevel($entity) { $primaryKey = $this->_getPrimaryKey(); $id = $entity; if ($entity instanceof EntityInterface) { $id = $entity->get($primaryKey); } $config = $this->config(); $entity = $this->_table->find('all') ->select([$config['path']]) ->where([$primaryKey => $id]) ->first(); if ($entity === null) { return false; } return substr_count($entity[$config['path']], '-') + 1; } /** * Returns a single string value representing the primary key of the attached table * * @return string */ protected function _getPrimaryKey() { if (!$this->_primaryKey) { $this->_primaryKey = (array)$this->_table->primaryKey(); $this->_primaryKey = $this->_primaryKey[0]; } return $this->_primaryKey; } }

我收到错误$this->addBehavior('ModernTree');

但是,如果我将Fatal error: Cannot redeclare class Cake\ORM\Behavior\ModernTreeBehavior in .../src/Model/Behavior/ModernTreeBehavior.php on line 0粘贴到内置文件/src/Model/Behavior/ModernTreeBehavior.php并加载TreeBehavior.php,那么一切都是如此。

你能告诉我原因吗?

1 个答案:

答案 0 :(得分:1)

您的类的名称空间声明是错误的,它应该是:

namespace App\Model\Behavior;