【问题标题】:Cannot redeclare class Cake\ORM\Behavior\ in CakePHP 3无法在 CakePHP 3 中重新声明类 Cake\ORM\Behavior\
【发布时间】:2015-05-18 04:13:11
【问题描述】:

我创建了src/Model/Behavior/ModernTreeBehavior.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;
    }
}

我在 CommentTables.php 中使用了$this-&gt;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 中并加载TreeBehavior.php,一切都很好。

你能告诉我原因吗?

【问题讨论】:

    标签: cakephp-3.0


    【解决方案1】:

    你的类的命名空间声明错误,应该是:

    namespace App\Model\Behavior;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 2013-08-07
      • 2011-03-22
      • 2023-03-29
      相关资源
      最近更新 更多