A place for users and developers of the Xataface to discuss and receive support.
by umair » Wed Nov 02, 2011 6:45 am
Hello Xataface universe
I am hoping you guys can help me out with a problem.
I have 3 tables as follows with their relationships
Student
Class.ClassID = student_class.ClassID student_class.StudentID = "$StudentID"
Class
Student.StudentID = student_class.StudentID student_class.ClassID = "$classID"
student_class (relational table)
This is what I need to do, before a user adds a relation between a student and class, I want to perform a validation on one of the fields in the student table for the student in question. What I need to check is....their is a field in the student table called SubPaid with two possible values of Yes and No. Before a student is added to class, I want to check if the SubPaid field for the student is set to Yes. If it is then allow the relationship and it is not then give error.
Now it would be easy to write a _validate function for this but I don't know where I would place it since relational tables don't seem to have a delegate class.
I have also looked at BeforeAddRelatedRecord function for delegate classes but I am not sure how to make it do what I need to do.
Any help on this matter would be greatly appreciated. Oh and I am using version 1.3rc6 2510 of xataface.
Thanks in advance.
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by shannah » Wed Nov 02, 2011 9:40 am
Sounds like you want to use the beforeAddRelatedRecord() delegate class method. - Code: Select all
function beforeAddRelatedRecord(/**Dataface_RelatedRecord*/ $record){ $parentRecord = $record->getParent(); // This is the parent record (i.e. the record from the base table. isa Dataface_Record) if ( /* some condition fails */ ){ return PEAR::raiseError('Some error occurred', DATAFACE_E_NOTICE); } }
This method goes in the delegate class for the base table (not the target of the relationship). -Steve
-
shannah
-
- Posts: 4457
- Joined: Wed Dec 31, 1969 5:00 pm
by umair » Sun Nov 06, 2011 3:44 am
Ok two problems, the relationship can be added through the sub-functions of both the student and class tabs, So when adding students from the class tab the beforeaddRelatedRecord doesn't get checked.
Second, even when the code is added to the delegate class of the student table, I cannot get the value for the field that I want to check against.
I would like to apologize in advance as I am still a complete beginner here.
Thanks
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by shannah » Mon Nov 07, 2011 10:01 am
the relationship can be added through the sub-functions of both the student and class tabs,
Not sure I understand. If you have 2 different relationships then of course one relationship won't run another relationship's triggers. 2 relationships means 2 sets of triggers. even when the code is added to the delegate class of the student table, I cannot get the value for the field that I want to check against
Why not?
-
shannah
-
- Posts: 4457
- Joined: Wed Dec 31, 1969 5:00 pm
by umair » Tue Nov 08, 2011 7:56 am
Well I can ignore the other relationship(might even remove it) since I am still trying to get the trigger to work with the first one.
In the student table delegate class I have put in the following:
function beforeAddRelatedRecord($record){ $parentRecord = $record->getParent(); //This correctly pulls the whole record for the student that I am trying to add the relationship, what I need is a specific field from the record //So after looking around the forums I tried //$parentRecord = $record->getParent('fieldname'); ------ fieldname is the exact name of the field in the student table in MySql database //$parentRecord = $record->getParent("fieldname"); //$parentRecord = $record->getParent($fieldname) ; ----- and all the combination of the three plus tried making another variable as follows //$res = $parentRecord->val("fieldname"); // with the same variations of the parameters for the ones above but cannot get the value of the field to be stored as the variable.
// This is the parent record (i.e. the record from the base table. isa Dataface_Record) if ( $res = Unpaid ){ //so this doesn't work return PEAR::raiseError('Some error occurred', DATAFACE_E_NOTICE); }
Basically I don't know how to get the value of a specific field from the record. I have tried var_dump() with all my attempts and none of catch the field.
I know my noobness must be getting annoying, but again any help is appreciated.
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by ADobkin » Tue Nov 08, 2011 9:18 am
I think your last attempt is basically correct: umair wrote://$res = $parentRecord->val("fieldname");
However, you don't have the value in quotes here, so perhaps that is the problem: umair wrote:if ( $res = Unpaid ){ //so this doesn't work return PEAR::raiseError('Some error occurred', DATAFACE_E_NOTICE); }
You could also try something like this for simple debugging before the if statement: - Code: Select all
echo 'RESULT: ' . $res
;
-
ADobkin
-
- Posts: 195
- Joined: Mon Oct 22, 2007 7:31 pm
- Location: Atlanta, GA, USA
by shannah » Tue Nov 08, 2011 10:16 am
- Code: Select all
if ( $res = Unpaid ){ //so this doesn't work
This will never work for two reasons: 1. If Unpaid is a constant then it will always return true because you are using assignment (=) and not equality comparison (==). 2. If Unpaid is not a constant then it will always be false because Unpaid doesn't exist. Perhaps you're looking for something like: - Code: Select all
if ( $res == 'Unpaid' ) {
-Steve
-
shannah
-
- Posts: 4457
- Joined: Wed Dec 31, 1969 5:00 pm
by umair » Tue Nov 08, 2011 11:20 am
Oh wow got that part to work thanks to you guys.
Ok now for the last part of my problem and I am keeping in mind that it may not be possible.
So I am wondering if there is a way to pull the record of the other table in the relationship. i.e.
I am adding student1 to class1, the record for student 1 is pulled using $record->getParent() Is there anyway I can get the record for class1 while still having the code in the student table delegate class?
or
Get the record of the relationship that is being created i.e.
Class_Student (table) StudentID - ClassID 1 1 2 1 3 1 (new record that is being created)
If I can get the record with studentID == 3 and classID == 1, I could get the rest of the field values using mysql_query
Thanks
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by shannah » Tue Nov 08, 2011 11:48 am
What is the relationship definition in the relationships.ini file?
Steve
-
shannah
-
- Posts: 4457
- Joined: Wed Dec 31, 1969 5:00 pm
by umair » Tue Nov 08, 2011 2:28 pm
The relational table is called student_class
Student -- relationship.ini
Class.ClassID = student_class.ClassID student_class.StudentID = "$StudentID"
Class -- relationship.ini
Student.StudentID = student_class.StudentID student_class.ClassID = "$classID"
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by umair » Thu Nov 10, 2011 9:47 pm
Ok I am going to take a guess and say that their is no way to pull record from the other table while adding relationships.
Thanks for the help.
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by shannah » Fri Nov 11, 2011 12:55 pm
Of course there is a way. df_get_record() function can load a single record from any table. df_get_records_array() function can load multiple records at a time.
-
shannah
-
- Posts: 4457
- Joined: Wed Dec 31, 1969 5:00 pm
by umair » Fri Nov 11, 2011 10:33 pm
Ok sorry I should have clarified what I was saying.
lets say with something like this:
df_get_record('class' , array('ClassID'=>##));
I can get the record for the class with ## ID, but I have to specify a static ID in that line. It doesn't pull from the record directly related to the relationship. so If I am adding Student with StudentID=>20 to Class with ClassID=>12, It won't dynamically pull the record of the class with ID of 12.
I could have something like:
function beforeAddRelatedRecord($record) { $parentRecord = $record->getParent(); $res = $parentRecord->val('StudentID'); $res2 = df_get_record('student_class', array('StudentID'=>$res)); $res3 = $res2->val('ClassID'); }
The value $res3 will have all the ClassIDs that are related to the student in the parentRecord.
I hope this was a bit more clear.
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
by shannah » Sun Nov 13, 2011 6:44 pm
Still not clear on what your problem is. You can load anything you want from the database using any method you choose (df_get_record, df_get_records_array, mysql_query, or any other method...). So I don't understand why there would be any information you couldn't retrieve. Your example posted has a few problems: - Code: Select all
$res = $parentRecord->val('StudentID'); $res2 = df_get_record('student_class', array('StudentID'=>$res)); $res3 = $res2->val('ClassID');
$res would contain the scalar integer StudentID of the parent record. $res2 would contain a Dataface_Record object encapsulating a row from the student_class table. $res2 would contain the scalar integer ClassID from the $res2 record. However if there are no records matching your query to load $res2 then $res2 could be null, which would cause the next line to have a fatal error because you'd be calling a method on a null value. It is still unclear to me what you are trying to achieve here. If you are adding a student to a class (as you mentioned earlier), then the parentRecord would be the class, and the class wouldn't have a StudentID field. So I assume then that this snippet is from the beforeAddRelatedRecord() method in the Students table (i.e. you are adding a class to a student). But then again... still not clear what you want to do here.
-
shannah
-
- Posts: 4457
- Joined: Wed Dec 31, 1969 5:00 pm
by umair » Tue Nov 15, 2011 11:47 am
Going to try again, At this point I have trigger in the Student delegate class to check a field from the parentRecord before allowing relationship. Thanks to you guys it work perfectly.
But as can be seen from the relationships.ini for both tables, the relationship goes both ways, I can either add a class to student, or student to class. The trigger since it is in student delegate class is called properly when I add classes to student, but since there is no trigger in the class delegate class, no validation is performed.
So my problem is getting a trigger for the class table delegate class, but since the field that needs to be checked is a student table field, I don't know how to call the record for the student being added to class.
-
umair
-
- Posts: 9
- Joined: Wed Nov 02, 2011 5:18 am
Return to Xataface Users
Who is online
Users browsing this forum: No registered users and 22 guests
|