Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Easy solution, I'm sure

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

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

Page: 1
Go to End
Author Topic: 1641 Views
  • UncleCheese
    Avatar
    4085 Posts

    Easy solution, I'm sure Link to this post

    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!!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Easy solution, I'm sure Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Easy solution, I'm sure Link to this post

    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.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Easy solution, I'm sure Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Easy solution, I'm sure Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Easy solution, I'm sure Link to this post

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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Easy solution, I'm sure Link to this post

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

    Anyone else have any clues?

    Sean

  • UncleCheese
    Avatar
    4085 Posts

    Re: Easy solution, I'm sure Link to this post

    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?

    1641 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.