How do I filter only related records in MJoin?
How do I filter only related records in MJoin?
nicoledramirez@hotmail.com
Posts: 60Questions: 14Answers: 2
I have the following code in my controller. When run, it pulls every record in the comment table instead of only those in the comment_access table with the correct cdi_master_id.
.MJoin(new MJoin("cdi_comment")
.Link("cdi_master.id", "comment_access.cdi_master_id")
.Link("cdi_comment.id", "comment_access.cdi_comment_id")
.Model<cdi_clinician_commentModel>()
.Order("cdi_comment.comment")
.Field(new Field("id")
.Options("cdi_comment", "id", "comment")
)
)
This discussion has been closed.
Answers
Edited by Allan for syntax highlighting
Hi,
Use the
Options
class to get the options, which has the option of providing a WHERE condition so you can limit the results.Allan
Hi Allan,
I tried that but I am not sure how to reference it correctly.
When I try something like:
.Field(new Field("id")
.Options("cdi_comment", "id", "comment", q => q.Where("cdi_comment.cdi_master_id", "cdi_master.id", "="))
I get an error: "Conversion failed when converting the nvarchar value 'cdi_master.id' to data type int." But it is already set as int datatype in the database.
You need to use the
Options
class as shown in the link, rather than just theField.Options()
method:Allan
I still get the same error.
Can you show me the code that you are using now please?
Thanks,
Allan
Thing is, if I do something like this:
It works. And both the cdi_comment.cdi_master and the cdi_master.id field are already integer fields in the db and model so I am not sure why it wants to convert.
Ah, you need to add
false
to yourWhere
method:Otherwise it tries to bind
cdi_master.id
as the value to be searched for, not a reference. That's to try and prevent SQL injection attacks.Allan
Close but not quite there yet. Now I get an error that cdi_master.id cannot be bound.
I have it in the Model:
Could you show me the exact error message please, and also the
Options
code that is now being used. Just so I can double check things on this side.Allan
The error message is cdi_master.id cannot be bound.
Thanks - I see the issue now.
The problem is that the
Options
class is constructing this SQL statement:That won't work since the
cdi_master
table is not referenced in that statement, thus throwing the error you are seeing.I'm not entirely certain what the goal is here? Are you trying to have the select list populated with different values for each row as it is being edited?
Thanks,
Allan
Basically I am trying to do this: https://editor.datatables.net/examples/advanced/joinArray.html but I am not sure why it isn't working.
The parent table is pulling from multiple tables and then passing back cdi_master_id to the form to pull all comment records into the form and allow the user to relate or unrelate them to the parent record.
The example that you linked to doesn't have any kind of query condition of the options that can be selected for the array field. Its simply a list of the options that are available.
If you drop the
where
clause, does it do what you are looking for?Thanks,
Allan