在yaml

时间:2016-12-18 17:13:52

标签: php symfony crud silex

我尝试使用symfony组件找到一种更快的方法来为现有数据库,CRUD,编写器方式生成UI。

找到CRUDlex。使用composer进行安装,同时设置CRUDlex sample 它工作正常,直到我在样本crud.yml

中添加新的表定义
category:
  label: Category
  table: category
  fields:
    name:
      type: text
      label: Name
      required: true
      unique: true  

在yml中添加了什么表,在访问http://localhost/crudlex/web/category

时总是抛出与此类似的错误
InvalidFieldNameException in AbstractMySQLDriver.php line 71: An exception occurred while executing 'SELECT COUNT(id) FROM `category` `category` WHERE deleted_at IS NULL':
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'deleted_at' in 'where clause'

完整的错误消息请查看下面的截图 CRUDlex Error

crudlex总是问“id”和“deleted_at”

代码与CRUDled示例index.php

相同
    $loader = require __DIR__.'/../vendor/autoload.php';

    //$loader->add('CRUDlex', __DIR__.'/../../CRUDlex/src');
    $app = new Silex\Application();

    $app['debug'] = true;

    $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
        'dbs.options' => array(
            'default' => array(
                'host'      => '127.0.0.1',
                'dbname'    => 'dbname',
                'user'      => 'root',
                'password'  => '',
                'charset'   => 'utf8',
            )
        ),
    ));
    $app->register(new Silex\Provider\SessionServiceProvider());

    $dataFactory = new CRUDlex\MySQLDataFactory($app['db']);
    $app->register(new CRUDlex\ServiceProvider(), array(
        'crud.file' => __DIR__ . '/../crud.yml',
        'crud.datafactory' => $dataFactory
    ));
    $app->register(new Silex\Provider\TwigServiceProvider());

    //$app['crud.layout'] = 'layout.twig';
    $app->mount('/', new CRUDlex\ControllerProvider());

    $app->match('/', function() use ($app) {
        return $app->redirect($app['url_generator']->generate('crudList', array('entity' => 'library')));
    })->bind('homepage');

    $app->run();

和文件夹结构

vendor
web
    > .htaccess
    > index.php
composer.json
crud.yml  

注意:我对silex和symfony2组件完全陌生^ _ ^ 谢谢,任何建议真的很感激

1 个答案:

答案 0 :(得分:2)

你缺少(至少)一个必需的元字段:

  • created_at(datetime,not null):创建记录时
  • updated_at(datetime,not null):上次编辑记录时
  • version(int,not null):记录的版本,每次编辑都会递增
  • deleted_at(datetime):如果不为null:当记录被(软)删除时

在您的情况下,完整的表创建SQL将如下所示:

CREATE TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `version` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;