1

I'm writing a script that loads all the news from our database using Propel2.
My current code looks as follows:

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

This gives me the following Output:

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
        )

)

As seen in the output above, the Author column returns the ID of the user that added it (via a relation to the Users table).
My goal is to get the Firstname and Lastname from UserDetails as the Author column instead of the ID of the user.
so the output will be as follows:

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
        )

)

This is my scheme at the moment: scheme

How can I accomplish this without making a new relation from News to UserDetails (unless absolutely needed)?

Small sidenote: I'm sorry if the flow in which I ask this question is a bit off, I have been looking for a solution for about two weeks now...

UPDATE: here is the schema file for this

<?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>
Finlay Roelofs
  • 533
  • 6
  • 21
  • Is UserDetails linked to Users with the delegate behavior? Or did you define the one-to-one relation manually? – chocochaos May 12 '18 at 15:35
  • I've created the one-to-one relation manually. I also can't find a delegation in the `schema.xml`, so I think it's safe to assume I did not link them with the delegate behaviour – Finlay Roelofs May 13 '18 at 20:11

0 Answers0