【问题标题】:Propel and related data using FOSUserbundle使用 FOSUserbundle 推进和相关数据
【发布时间】:2013-03-20 13:14:44
【问题描述】:

我正在使用 FOSUserBundle,并创建了一个包含两列的 Message 表,它们与 FOSUserBundle 中的用户相关。

我想使用一个查询来获取有关作者的消息和信息。

以下 schema.xml:

<?xml version="1.0" encoding="UTF-8"?>

<database name="default" namespace="Acme\StoreBundle\Model" defaultIdMethod="native">

<table name="message">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="title" type="varchar" size="64" required="true" defaultValue="(untitled)"/>
    <column name="content" type="longvarchar" />
    <column name="author_id" type="integer" required="true" />
        <foreign-key foreignTable="fos_user">
            <reference local="author_id" foreign="id" />
        </foreign-key>
    <column name="recipient_id" type="integer" required="true" />
        <foreign-key foreignTable="fos_user">
            <reference local="recipient_id" foreign="id" />
        </foreign-key>
</table>
</database>

我尝试了很多选择,但没有结果。

当我尝试时,似乎合乎逻辑:

$messages = MessageQuery::create()
            ->join('Message.Authorid')
            ->findByRecipientId(1);

我收到一个错误:“未知表或别名消息”

与:

->join('Authorid')

错误:“Acme\StoreBundle\Model\Message 表上的未知关系 Authorid”

我做错了什么?

感谢您的帮助。

【问题讨论】:

    标签: symfony join foreign-keys propel fosuserbundle


    【解决方案1】:

    好的,我找到了。

    作者和收件人的架构必须如下所示:

    ...
    <column name="author_id" type="integer" required="true" />
        <foreign-key foreignTable="fos_user" phpName="Author">
            <reference local="author_id" foreign="id" />
        </foreign-key>
    
    <column name="recipient_id" type="integer" required="true" />
        <foreign-key foreignTable="fos_user" phpName="Recipient">
            <reference local="recipient_id" foreign="id" />
        </foreign-key>
    ...
    

    重要的部分是 phpName。

    现在是控制器。这里发生了一些奇怪的事情,但不会打扰。

    $messages = MessageQuery::create()
        ->find();
    
    return $this->render('AcmeStoreBundle:Message:index.html.twig',
            array('messages' => $messages)
        );
    

    在这个简单的代码之后,我可以访问来自 twig 的所有内容。

    在这一点上,我想展示一个例子:

    当控制器看起来像上面那样时,那么

    # AcmeStoreBundle:Message:index.html.twig
    
    messages
    

    显示消息表的每个字段,例如:

    Acme\StoreBundle\Model\Message_0: Id: 1 CreatedAt: !!php/object:O:8:"DateTime":3:    {s:4:"date";s:19:"2013-03-20 13:00:00";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";} Title: (untitled) Content: 'test content' AuthorId: 1 RecipientId: 2 Acme\StoreBundle\Model\Message_1: Id: 2 CreatedAt: !!php/object:O:8:"DateTime":3:{s:4:"date";s:19:"2013-03-20 13:15:22";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";} Title: (untitled2) Content: 'bla bla content 2' AuthorId: 2 RecipientId: 1 
    

    如您所见,与作者或收件人的关系没有任何关系,但是当我调用 message.author.username 或 message.recipient.username(在消息的 for 循环中)时,我得到了这个。这正是我的预期。

    现在控制器看起来像这样:

    $messages = MessageQuery::create()
            ->joinWith('Author')
            ->find();
    
    return $this->render('AcmeStoreBundle:Message:index.html.twig',
            array('messages' => $messages)
        );
    

    messages (in twig) 会抛出上面的所有字段,以及一个作者对象(称为作者,而不是用户,因为架构中的 phpName)。

    感谢大家阅读我的问题并感谢大家的参与。

    【讨论】:

      【解决方案2】:

      join 方法使用表或 phpName 别名而不是字段来获取关系。所以你可能想要:

      ->join("Message.Author")
      

      或者,正如您的外键所暗示的那样,也许是这个?

      ->join("Message.FosUser")
      

      更新

      对于同一个表的两个 FK 引用,您将需要使用外键标记的 phpNamerefPhpName 属性:

      <table name="message">
        ...
        <foreign-key foreignTable="fos_user" phpName="Author" refPhpName="AuthoredMessage">
          <reference local="author_id" foreign="id" />
        </foreign-key>
        <foreign-key foreignTable="fos_user" phpName="Recipient" refPhpName="ReceivedMessage">
          <reference local="recipient_id" foreign="id" />
        </foreign-key>
      </table>
      

      那你可以试试-&gt;join('Message.Author')或者-&gt;join('Message.Recipient')

      请参阅documentation of the foreign-key element

      【讨论】:

      • 未知表或别名消息 500 内部服务器错误 - PropelException 在这两种情况下。 ;/
      • 我认为您需要找出其他类的名称......如果它是User,那么点后面的部分(.)应该是User。我认为你说对了。无论如何,需要知道 Propel 认为连接类是什么。哦!但我看到您对同一张表有两个 FK 引用......可能有问题。查看我的更新。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多