如何从Propel2中的非直接关系中获取值?

时间:2018-04-10 09:40:15

标签: php propel2

我正在编写一个脚本,使用Propel2从我们的数据库中加载所有新闻 我目前的代码如下:

$news = NewsQuery::create()->find(); 

这给了我以下输出:

Array
(
    [0] => Array
        (
            [Id] => 1
            [Title] => Test News
            [Content] => hueheueheeueheu
            [CreatedAt] => 2018-03-29T00:00:00+00:00
            [Author] => 2
        )

    [1] => Array
        (
            [Id] => 2
            [Title] => gfdgdsfgfdg
            [Content] => sdfgfdgfdgfdgf
            [CreatedAt] => 2018-03-29T00:00:00+00:00
            [Author] => 2
        )
    [2] => Array
        (
            [Id] => 3
            [Title] => abcdef
            [Content] => ghijklm
            [CreatedAt] => 2018-03-29T00:00:00+00:00
            [Author] => 3
        )

)

如上面的输出所示,Author列返回添加它的用户的ID(通过与Users表的关系)。
我的目标是将Firstname LastnameUserDetails作为Author列而不是用户的ID。 所以输出如下:

Array
(
    [0] => Array
        (
            [Id] => 1
            [Title] => Test News
            [Content] => hueheueheeueheu
            [CreatedAt] => 2018-03-29T00:00:00+00:00
            [Author] => John Doe
        )

    [1] => Array
        (
            [Id] => 2
            [Title] => gfdgdsfgfdg
            [Content] => sdfgfdgfdgfdgf
            [CreatedAt] => 2018-03-29T00:00:00+00:00
            [Author] => John Doe
        )
    [1] => Array
        (
            [Id] => 3
            [Title] => abcdef
            [Content] => ghijklm
            [CreatedAt] => 2018-03-29T00:00:00+00:00
            [Author] => Alice Smith
        )

)

这是我目前的计划: scheme

如果不建立NewsUserDetails的新关系(除非绝对必要),我怎样才能实现这一目标?

小旁注: 我很抱歉,如果我提出这个问题的流程有点过时,我一直在寻找解决方案大约两周......

更新:这是此

的模式文件
<?xml version="1.0"?>
<database name="MijnWerkwent">
  <table name="Users">
    <column name="ID" type="Integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="Username" type="Varchar" size="255" required="true"/>
    <unique name="IX_UQ_Users_ID">
      <unique-column name="ID"/>
    </unique>
  </table>
  <table name="UserDetails">
    <column name="UID" type="Integer" required="true" primaryKey="true"/>
    <column name="Firstname" type="Varchar"/>
    <column name="Surname" type="Varchar"/>
    <column name="Email" type="Varchar"/>
    <column name="Address" type="Varchar"/>
    <column name="PostalCode" type="Varchar"/>
    <column name="Telephone" type="Varchar"/>
    <column name="City" type="Varchar"/>
    <unique name="IX_UQ_UserDetails_UID">
      <unique-column name="UID"/>
    </unique>
    <foreign-key foreignTable="Users" onDelete="CASCADE">
      <reference foreign="ID" local="UID"/>
    </foreign-key>
  </table>
  <table name="Authtokens">
    <column name="ID" type="Integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="Token"/>
    <column name="UID" type="Integer" required="true"/>
    <unique name="IX_UQ_Authtokens_ID">
      <unique-column name="ID"/>
    </unique>
    <foreign-key foreignTable="Users" onDelete="CASCADE">
      <reference foreign="ID" local="UID"/>
    </foreign-key>
  </table>
  <table name="Passwords">
    <column name="ID" type="Integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="Seed" type="BigInt"/>
    <column name="Password" type="Varchar"/>
    <column name="UID" type="Integer" required="true"/>
    <column name="TID" type="Integer" required="true"/>
    <unique name="IX_UQ_Passwords_ID">
      <unique-column name="ID"/>
    </unique>
    <unique name="IX_UQ_Passwords_TID">
      <unique-column name="TID"/>
    </unique>
    <foreign-key foreignTable="Users" onDelete="CASCADE">
      <reference foreign="ID" local="UID"/>
    </foreign-key>
    <foreign-key foreignTable="Authtokens" onDelete="CASCADE">
      <reference foreign="ID" local="TID"/>
    </foreign-key>
  </table>
  <table name="News">
    <column name="ID" type="Integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="Title" type="Varchar"/>
    <column name="Content" type="CLOB"/>
    <column name="Created_At" type="Timestamp"/>
    <column name="Author" type="Integer"/>
    <unique name="IX_UQ_News_ID">
      <unique-column name="ID"/>
    </unique>
    <foreign-key foreignTable="Users" onDelete="SETNULL">
      <reference foreign="ID" local="Author"/>
    </foreign-key>
  </table>
</database>

0 个答案:

没有答案