简体   繁体   English

为什么Doctrine ORM不会为我的一对多模型创建SQL查询?

[英]Why will Doctrine ORM will not create SQL query for my 1-to-many model?

I know this has to have an easy answer, but I cannot figure it out. 我知道这必须有一个简单的答案,但我无法弄清楚。 After tears, I am hoping someone here can help. 泪流满面,我希望这里的人能为您提供帮助。

Here is my YML model: 这是我的YML模型:

Identity:
  columns:
    id:
      type:           integer(10)
      primary:        true
      autoincrement:  true
    username:
      type:           string(255)

Profile:
  columns:
    id:
      type:           integer(10)
      primary:        true
      autoincrement:  true
    identity_id:
      type:           integer(10)
    profiletype_id:
      type:           integer(10)
    name:
      type:           string(255)
  relations:
    Identity:
      local:          identity_id
      foreign:        id
      class:          Identity
      foreignAlias:   Profiles
    Profiletype:
      local:          profiletype_id
      foreign:        id
      class:          Profiletype
      foreignAlias:   Profiles

Profiletype:
  columns:
    id:
      type:           integer(10)
      primary:        true
      autoincrement:  true
    type:
      type:           string(255)

As you can see, 3 linked tables are generated: 如您所见,将生成3个链接表:

Identity 身分识别
- can have multiple Profiles -可以有多个配置文件

Profile 轮廓
- has one Identity -具有一个身份
- has one Profiletype -具有一个配置文件类型

Profiletype 配置文件类型
- can have multiple Profiles -可以有多个配置文件

Now, In my code, I can perform queries on the generated models for Identity and Profiletype. 现在,在我的代码中,我可以对生成的Identity和Profiletype模型进行查询。

For example: 例如:

        $q = Doctrine_Query::create()
          ->select('i.*')
          ->from('Identity i');
        echo $q->getSqlQuery();

will work and produce: 将工作并产生:

SELECT i.id AS i__id, i.username AS i__username FROM identity i

However, when I go to run any query on the Profile table, it will not work. 但是,当我在Profile表上运行任何查询时,它将无法工作。 Even a simple query such as 甚至是一个简单的查询,例如

        $q = Doctrine_Query::create()
          ->select('p.*')
          ->from('Profile p');
        echo $q->getSqlQuery();

fails. 失败。 I have tried changing the class name in the FROM to 'Profiles' instead of 'Profile'. 我尝试将FROM中的类名更改为“个人资料”,而不是“个人资料”。 Still nothing. 依然没有。

Any ideas on what I'm doing wrong. 关于我在做什么的任何想法。

After changing every possible variable and tracing through my model line-by-line, I came to an embarrassing discovery: 更改所有可能的变量并逐行跟踪模型后,我发现了一个令人尴尬的发现:

The controller I was calling the Doctrine Query from was called profile.php. 我从中调用“ Doctrine查询”的控制器称为profile.php。

profile.php profile.php

<?php
class Profile extends Controller {

    function Profile()     {
        parent::Controller();
    }

    function index()     {
        $q = Doctrine_Query::create()
            ->from('Profile');
        echo $q->getSqlQuery();
    }

}

Ass soon as I named the controller something else other than 'profile', it all suddenly worked. 屁股当我将控制器命名为“配置文件”以外的其他名称时,一切突然起作用了。 Eg, naming it to docile.php: 例如,将其命名为docile.php:

docile.php docile.php

<?php
class Docile extends Controller {

    function Docile()     {
        parent::Controller();
    }

    function index()     {
        $q = Doctrine_Query::create()
            ->from('Profile');
        echo $q->getSqlQuery();
    }

}

'Profile' does not appear to be a reserved word in CodeIgniter ( link text ). “配置文件”在CodeIgniter( 链接文本 )中似乎不是保留字。 It does appear, however, that you cannot call a doctrine 'table class' from a controller class of the same name. 但是,确实出现了您无法从同名的控制器类中调用一个学说的“表类”的情况。

Thanks for your input guys. 感谢您的投入。 Hope this saves someone else similar hassles. 希望这可以节省其他类似的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM