【问题标题】:Doctrine search index models not generated by generateModelsFromYaml()不是由 generateModelsFromYaml() 生成的教义搜索索引模型
【发布时间】:2011-06-15 10:47:00
【问题描述】:

我正在尝试使用 Doctrine 1.2 的搜索行为为我的项目构建一个搜索引擎,我按照手册的建议构建了一个 YAML,并引用了我的搜索字段。 并调用 generateModelsFromYaml 函数创建我的 php 模型,所有 php 模型都创建没有错误但搜索索引表的模型丢失了......

我的 YAML 文件的摘录:

DIFFichier:
  tableName: ens_diffusion.DIF_Fichiers
    columns:
      DOC_Id:
        type: integer(4)
        primary: true
        notnull: true
        autoincrement: true
      DOC_Categorie:
        type: integer(4)
        default: 0
      DOC_Description:
        type: string(256)
      DOC_Adresse:
        type: string(256)
        options:
          charset: utf8
          type: InnoDB
    actAs:
      Searchable:
      fields: [DOC_Description] 

模型中生成了可搜索的字段,这里我正确生成了一个名为“DIFFichier”的模型,但缺少对应的索引表“d_i_f_fichier_index”

    $searchable0 = new Doctrine_Template_Searchable(array(
         'fields' => 
         array(
          0 => 'DOC_Description',
         ),
         ));
    $this->actAs($searchable0);

我以这种方式生成我的 PHP 模型

include_once "Doctrine-1.2.3/Doctrine.php";
spl_autoload_register(array('Doctrine', 'autoload'));

    Doctrine::generateModelsFromYaml(
    'diffusion2.yml', 
    'C:\Documents and Settings\admin\Desktop\modelsDoctrine', 
    array(
        'doctrine'
    ), 
    array(
        'classPrefix' => 'Diffusion_Model_', 
        'classPrefixFiles' => false
    )
);

我仍然无法生成我的索引表,还有其他方法可以从我的 YAML 文件生成我的表吗?

【问题讨论】:

    标签: php mysql search doctrine


    【解决方案1】:

    我终于找到了答案:

    首先,索引表好像不是generateModelsFromYaml()方法生成的,更有可能是Doctrine根据需要直接在数据库上创建的……

    所以我尝试通过在我之前生成的模型上使用 generateSqlFromArray() 手动生成它们,但是 Doctrine 在创建主键字段时抛出异常。经过一番研究,我发现在创建索引表的情况下,索引的主键必须是小写的。

    但是数据库的方案被锁定了。所以我找到了一个解决方法:

    • 我在我的 YAML 文件上运行了一个小的正则表达式,以使我的所有主键都小写,所以像“IND_Id”这样的键变成了“ind_id”。

    • 我使用新的 YAML 模板再次生成了我的模型。

    • 我在新创建的模型上调用了generateSqlFromArray()方法,提取了索引表的SQL CREATE。

    • 我将此 SQL 代码粘贴到我的数据库中

    现在一切正常,即使是我的原始模型,尽管它们的主键仍然是大写的......

    这是我创建的用于小写主键的脚本

    $src = file_get_contents("source.yml");
    
    //Lower case the primary key
    $src = preg_replace("/([A-Z]{3}_[A-Z]{1}[a-z]*:)/e", "strtolower('\\1')", $src);
    
    file_put_contents("source_lower.yml", $src);
    

    现在是生成我的索引表的 SQL 创建代码的脚本

    require_once("../config.php");
    
    $tables = array();
    $dirname = '../include/models/generated/';
    $dir = opendir($dirname); 
    
    //Extract the classname from the filenames
    while($file = readdir($dir)) {
        if($file != '.' && $file != '..' && !is_dir($dirname.$file)){
    
            $classe = substr($file, 4, strlen($file) -8);
            $tables[] = $classe;
        }
    }
    
    //Generate SQL create for index tables
    foreach($tables as $t){
        $sql = Doctrine_Core::generateSqlFromArray(array($t));
        afficher($sql[1]);
    }
    
    //Print the SQL code if CREATE
    function afficher($str){
        if(substr($str, 0, 6) == "CREATE"){
            echo $str.";<br/>";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 2020-02-14
      • 2013-07-11
      • 2014-10-20
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      相关资源
      最近更新 更多