Jump to content

Set Relation with where clause

  • Please log in to reply
3 replies to this topic

#1 arve



  • Advanced Member
  • Pip
  • 8 posts
  • LocationNorway

Posted 13 January 2018 - 02:27 PM



I'd like to set a where clause into my relationship. I've done this;

crud->setRelation('1', '2', '3', NULL, array('assembled'=>0));

Have also tried with table.assembled w/o any luck. 


Seems like I get all the values that are 1 as well as 0. 


Can someone help me on this one?


Thanks alot guys!

Best Regards


#2 arve



  • Advanced Member
  • Pip
  • 8 posts
  • LocationNorway

Posted 14 January 2018 - 10:58 AM

Okay, so after some looking into the code I did find that it was the other way around. 

public function setRelation($fieldName , $relatedTable, $relatedTitleField, $where = null, $orderBy = null)

So I did move the order of the 2 last attributes, that did work perfect! 


But now I've got 2 columns that relates to the same table and then I get this error;

Message: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'tbl_tablename'

How do I manage to set an alias on these 2 or work around this, anybody knows?

Best Regards


#3 web-johnny


    grocery CRUD Author

  • Administrators
  • 1,155 posts
  • LocationLondon

Posted 30 January 2018 - 04:33 AM

Hello @arve,


I will have a more specific example for you to help. Please let me know if that worked:

$crud->setRelation('office','offices','phone', [
    'offices.city = ?' => 'San Francisco'


As you can see from the above code I am also including the table name (e.g. offices) and the syntax that I am writing the where is with the syntax here: https://www.groceryc...on-list/where-3


Please let me know if you are still having issues.




Posted Image

#4 flowerbed333



  • Members
  • Pip
  • 5 posts

Posted 22 February 2018 - 02:55 PM

Hi Johnny,


I have also purchased Grocery Crud Enterprise and I have an issue with usage of Set Relation (or Default Ordering) when I use the Syntax.

Example :

$crud->setRelation('lmc_title','lm_parameters','p_value', [
    'lm_parameters.p_type' => 'cust_title'


I have no error when form is generated but I have One record Empty.


When I remove the Where clause ... All lines are generated but without filtering


Just for info I have the same problem with default Ordering

$crud->defaultOrdering('p_type', 'asc');
This Works !!!
   'p_type' => 'as'

This works Not ... But the 2 are similars. I am on XAMPP with GC Entr 2.5.


Could you give me a Trick ?

Attached Thumbnails

  • Capture d’écran 2018-02-22 à 15.54.25.png

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users