在PHP中使用Spot ORM时出错

时间:2018-05-22 05:04:32

标签: php orm

我正在使用Spot ORM(http://phpdatamapper.com)。 问题:代码中途终止。 关于如何使框架转储错误以进行调试的文档很少。代码在这里:

namespace Entity;
namespace Config;


require 'vendor/autoload.php';

echo "hello";
set_time_limit(0);
error_reporting(E_ALL);



class Post extends \Spot\Entity
{
    protected static $table = 'posts';
    public static function fields()
    {
        return [
            'id'           => ['type' => 'integer', 'primary' => true, 'autoincrement' => true],
            'title'        => ['type' => 'string', 'required' => true],
            'body'         => ['type' => 'text', 'required' => true],
            'status'       => ['type' => 'integer', 'default' => 0, 'index' => true],
            'author_id'    => ['type' => 'integer', 'required' => true],
            'date_created' => ['type' => 'datetime', 'value' => new \DateTime()]
        ];
    }
}

class Url extends \Spot\Entity
{
    protected static $table = 'urls';
    public static function fields()
    {
        return [
            'id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true],
            'domain'           => ['type' => 'text'],
            'url'        => ['type' => 'text', 'required' => false],
            'classification1'         => ['type' => 'text', 'required' => false],
            'section' => ['type' => 'string', 'required' => false],
            'status'       => ['type' => 'text'],
         /*   'author_id'    => ['type' => 'integer', 'required' => true],
            'date_created' => ['type' => 'datetime', 'value' => new \DateTime()]
        ];
    }
}

$cfg = new \Spot\Config();

$spot = new \Spot\Locator($cfg);

$cfg->addConnection('mysql', [
    'dbname' => 'sotereading',
    'user' => 'root',
    'password' => 'root',
    'host' => 'localhost',
    'driver' => 'pdo_mysql',
]);

echo "hello";

/* this line has a problem */
$mapper = $spot->mapper('Entity\Post');
echo "hello";
$mapper->migrate();
echo "hello";

我使用简单的“echo”语句来缩小给出问题的那一行,我找到了它(参见上面代码中的/ * * /中的注释)。为什么这会给出问题?无论我使用'Entity \ Post'还是'Entity \ Url',它仍然是一样的。

1 个答案:

答案 0 :(得分:0)

对于初学者,您有一个双重名称空间声明,因此Post实际上是Config \ Post而不是Entity \ Post。

<?php
namespace Entity;
namespace Config;

class Post { }

$x = new Post();

echo "Class is: ", get_class($x);

?>

Class is: Config\Post