Page 1 of 1

Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 07, 2010 6:03 pm
by 00Davo
This isn't really a problem with Xataface, but any assistance would be most welcome. I've set up a category checkbox system for one of my tables, almost exactly as described in method 3 here.

Now I want to implement the relationships to that table in a very specific way. I've got two relevant relationship tabs, both of which are many-to-many. I'd like these tabs to only be accessible (and, conversely, the record to only appear on the other end of the relationship) if a specific category is selected in the aforementioned category system (different category for each tab). I just can't figure out the SQL for such a query. :(

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Mon Jan 11, 2010 11:57 am
by shannah
One nice way to hide/show a relationship tab conditionally is using the action:condition attribute of the relationship in the relationships.ini file. Here is an example:

Code: Select all
[courses_taught]
__sql__ = "select * from courses where instructor='$person_id'"
action:condition = "!in_array($record->val('category'), array(8,9,11))"
action:label = "Courses (Instructor)"

[courses_tad]
__sql__ = "select * from courses c inner join course_tas as ct on c.course_id=ct.course_id where ct.person_id='$person_id'"
action:condition = "!in_array($record->val('category'), array(1,4,5,6,11))"
action:label = "Courses (TA)"


I have 2 relationships here in the people table. If the current record (person) is a teacher, I want the courses_taught relationship to show up. If the current record (person) is a grad student, I want the courses_tad to show up (i.e. the course that they are teaching assistants for). You can see the action:condition directive that checks the category of the current person to see if they are in the appropriate category.

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 14, 2010 6:57 pm
by 00Davo
Thanks for the assistance, as always :D - this solves exactly half the problem.

The other half of the problem: it's a many-to-many relationship, and you can add records from either "end". Adding this condition (to my Adult table) prevents me from adding a class to a non-tutor adult (perfect!), but I can go to the class, and add the adult from there with Add Existing Record (not so perfect).

What do I need to do to fix this bit? Ideally, the existing-record list on the Class end would only display adults that satisfy the condition. :?

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 14, 2010 10:18 pm
by shannah
You can customize the list of records that can be added to a relationship via the add existing dialog, by either by specifying the 'vocabulary:existing' directive in the relationship defintion, or by defining the {relationshipname}__getAddableValues() in your delegate class.

Defining the vocabulary existing attribute might look as follows:

valuelists.ini file:
Code: Select all
[my_addable_values]
__sql__ = "select id, name from foo where can_be_added=1"
[code]

relationships.ini file:
[code]
[my_relationship]
__sql__ = "..."
vocabulary:existing="my_addable_values"


-Steve

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 14, 2010 11:12 pm
by 00Davo
Unfortunately, I'm having problems again, and need to ask for more help. :(

I've implemented the files like this.
relationships.ini:
Code: Select all
[Tutors]
   adult.AdultID = classes.AdultID
   classes.ClassID = "$ClassID"
   vocabulary:existing = "Tutors"

valuelists.ini
Code: Select all
[Tutors]
   __sql__ = "SELECT Adult.AdultID, CONCAT(LName, ', ', FName) FROM adult INNER JOIN AdultRole ON Adult.AdultID = AdultRole.AdultID AND AdultRole.RoleID = 3"

When I go to add a related record, I get PHP errors at the top:
Code: Select all
Warning: array_keys() expects parameter 1 to be array, null given in C:\xampp\xampp\htdocs\newypt\xataface\Dataface\Relationship.php on line 1255

Warning: Invalid argument supplied for foreach() in C:\xampp\xampp\htdocs\newypt\xataface\Dataface\Relationship.php on line 1255

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\xampp\htdocs\newypt\xataface\Dataface\Relationship.php:1255) in C:\xampp\xampp\htdocs\newypt\xataface\Dataface\Application.php on line 1603

And a Xataface error:
Code: Select all
Errors
There are no records that can be added to this relationship.

If I run the same SQL query directly, it returns plenty of records. Additionally, if I use the valuelist in a more "traditional" manner, with fields.ini, it returns records just fine. It's just the relationship that won't work with my query.

I'm having problems. Again. I'm not really very good at this stuff. :(

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 14, 2010 11:25 pm
by shannah
In which table's valuelists.ini file did you define the valuelist? And in which table's relationships.ini file did you define the relationship?

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 14, 2010 11:26 pm
by 00Davo
shannah wrote:In which table's valuelists.ini file did you define the valuelist? And in which table's relationships.ini file did you define the relationship?

Whoops, sorry. Both are in the /tables/class folder.

Re: Only allow relationship based on "category" checkboxes?

PostPosted: Thu Jan 14, 2010 11:49 pm
by 00Davo
Experimentally, I copied the valuelist definition into /tables/adult/valuelists.ini, and it works now. So the destination has to define the valuelist, apparently. Anyway, it works. Yay. :D