LeonardChallis
LeonardChallis

Reputation: 7783

Why aren't these propel many-to-many relationships working?

I am following this documentation: http://www.propelorm.org/documentation/04-relationships.html#manytomany_relationships

I have a very similar set up (albeit a little simplified for readability):

Logbook

  <table name="logbook" phpName="Logbook" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" size="10" sqlType="int(10) unsigned" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="location" phpName="Location" type="VARCHAR" size="255" required="false"/>
  </table>

Contact

  <table name="contact" phpName="Contact" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" size="10" sqlType="int(10) unsigned" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="title" phpName="Title" type="VARCHAR" size="255" required="true"/>
    <column name="name" phpName="Name" type="VARCHAR" size="255" required="true"/>
    <column name="telephone" phpName="Telephone" type="VARCHAR" size="20" required="true"/>
  </table>

Logbook Contact

  <table name="logbook_contact" phpName="LogbookContact" idMethod="native" isCrossRef="true">
    <column name="logbook_id" phpName="LogbookId" type="INTEGER" size="10" sqlType="int(10) unsigned" primaryKey="true" required="true"/>
    <column name="contact_id" phpName="ContactId" type="INTEGER" size="10" sqlType="int(10) unsigned" primaryKey="true" required="true"/>
    <foreign-key foreignTable="contact" name="fk_logbook_contact_contact" onDelete="CASCADE" onUpdate="CASCADE">
      <reference local="contact_id" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="logbook" name="fk_logbook_contact_logbook" onDelete="CASCADE" onUpdate="CASCADE">
      <reference local="logbook_id" foreign="id"/>
    </foreign-key>
    <index name="fk_logbook_contact_contact">
      <index-column name="contact_id"/>
    </index>
  </table>

Notice the isCrossRef="true" on the logbook_contact table, which shows this is a junction table. From here I have re-run propel-gen om and even propel-gen convert-conf (after clearing out the build directory) but I still don't seem to be able to do something like this:

<?php
/* presume $logbook has been LogbookQuery::create()'d already... */
$contacts = $logbook->getContacts();
$nbContacts = $logbook->countContacts();

I use Zend Studio, and I don't see it in the code-hint window:

No $logbook->getContacts();

Can anyone suggest to me what I might have done wrong here? I'm happy for you to point me to docs - I have followed these as closely as possible and everything up until $test works just fine.

Upvotes: 1

Views: 919

Answers (1)

William Durand
William Durand

Reputation: 5519

You probably want to call getContacts() on a Logbook instance, not on a PropelCollection. See your code, it seems you fetch a collection of Logbook instead of an instance:

$this->logbook = $this->company->getLogbooks();

Here, $this->logbook seems to be a PropelCollection regarding the autocompletion output. It's not a Logbook object. You can try:

$test = $this->logbook[0]->getContacts(); 

Upvotes: 2

Related Questions