Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Easy solution, I'm sure


Reply

8 Posts   1649 Views

Avatar
UncleCheese

30 October 2007 at 5:40am (Last edited: 30 October 2007 5:41am), 4085 Posts

I have an Attorney Page that has a has_many relationship with Schools. The user adds schools to an attorney through a TableField. In order to tie specific schools to one attorney, I've used the setExtraData() method to put 'AttorneyPageID => $this->ID' on the table.

This works great until I add a new attorney, and I get a SQL error! The $this->ID is of course returning a junk ID... something like "new-attorney-page-id," or something to that effect, and now my filter is erroneous.. e.g. "WHERE AttorneyPageID ='new-attorney-page-id' "..

So i hacked something:

   function getTheID() {
      return (is_integer($this->ID)) ? $this->ID : 9999;
   }

But of course this wont' work in the long run... please help!!

Avatar
Willr

30 October 2007 at 8:48pm Forum Moderator, 5511 Posts

perhaps I didnt quite understand correctly but on the school class you need to make sure you have a $has_one attorney. Anything like $has_many, $many_many the child class's need the relationship as well so $has_one, $belongs_many_many etc

Avatar
UncleCheese

31 October 2007 at 3:09am 4085 Posts

I do have that relationship set up correctly. Let me be more specific. Here's the error:

Error: DATABASE ERROR: Couldn't run query: SELECT `Education`.*, `Education`.ID, if(`Education`.ClassName,`Education`.ClassName,'Education') AS RecordClassName, School_Name FROM `Education` WHERE (Education.AttorneyPageID = new-AttorneyPage-24-1) | Unknown column 'new' in 'where clause'
At line 386 in /home/virtual/bpflegal.com/httpdocs/sapphire/core/model/Database.php

As you can see, it's trying to run a WHERE clase on the invalid id "new-AttorneyPage-24-1"

Here's the table that's running that query:

    $EducationField = new TableField(
'Education',
'Education',
array(
    'School_Name' => 'School Name'
),
array(
    'School_Name' => 'TextField'
),
null,
      "Education.AttorneyPageID = " . $this->ID
);

The filter "Education.AttorneyPageID = " . $this->ID is not returning a valid ID when the page is new.

Avatar
Sean

31 October 2007 at 7:33pm (Last edited: 31 October 2007 7:38pm), Forum Moderator, 921 Posts

That's because the given page doesn't have an ID until it's first saved. You could probably optionally include the last parameter like this, so it safely passes null where $this->ID that isn't a number.

if(is_numeric($this->ID)) {
$filter = "Education.AttorneyPageID = " . $this->ID;
} else {
$filter = null;
}

Then, include $filter as the sixth parameter for your TableField object.

Hope this helps!

Cheers,

Sean

Avatar
UncleCheese

1 November 2007 at 5:33am 4085 Posts

Right, but the problem with that is that now when I create a new attorney, I go to add schools and I get a comprehensive list of every school that has ever been entered for an attorney, because the list isn't filtered. When I add a new attorney, I want the list of schools to be blank to start.

This has to be a common issue. I'm really confused, here.

Avatar
UncleCheese

6 November 2007 at 3:44am 4085 Posts

Any other thoughts on this? I really feel like I'm missing something. It's too simple a problem to not have an answer.

Avatar
Sean

6 November 2007 at 8:56am Forum Moderator, 921 Posts

I'm not sure - when a page isn't saved there's certain things you can't do.

Anyone else have any clues?

Sean

Avatar
UncleCheese

6 November 2007 at 9:58am 4085 Posts

The ID before the page is saved is "new-AttorneyPage-24-1"... This leads me to believe that it has hit the DB to get the insertion ID of 24.. Not sure what the 1 is for, but could I somehow parse out this string to get just the 24 and use that as the ID?