Asked  2 Years ago    Answers:  5   Viewed   70 times

I have the problem with the sequence of joins. The similar problem was in another question Manipulating Order of JOINS in CakePHP. The answer was to use Containable behavior. In my case that is unacceptable because I have deeper associations and containable generates too many queries. Containable does not generate joins for the three level associations. It generates additional queries for every entry from the second level table.

My query is:

$this->LevelOne->find('all', array(
    'joins' => array(array(
         'table' => 'level_three',
         'alias' => 'LevelThree',
         'type' => 'LEFT',
         'conditions' => array(
              'LevelThree.id = LevelTwo.level_three_field_id'
          )
     ))
));

The problem here is that cake generates several joins but the join of the LevelThree table is done before the joins of the LevelTwo tables and that throws an SQL error "Unknown column 'LevelTwo.level_three_field_id' in 'on clause'". If the LevelThree join would be at the end of the query after all LevelTwo joins the query would be okay.

So, the question is how to change the sequence of joins?

 Answers

2

Finally I figured out how to do that:

$this->LevelOne->unbindModel(array('belongsTo' => array('LevelTwo')));
$this->LevelOne->find('all', array(
    'joins' => array(
          array(
             'table' => 'level_two',
             'alias' => 'LevelTwo',
             'type' => 'LEFT',
             'conditions' => array(
                  'LevelTwo.id = LevelOne.level_two_field_id'
              )
          ),
          array(
             'table' => 'level_three',
             'alias' => 'LevelThree',
             'type' => 'LEFT',
             'conditions' => array(
                  'LevelThree.id = LevelTwo.level_three_field_id'
              )
          )
     )
));
Sunday, September 4, 2022
 
5

You could try something like this:

<?php  
class Group extends AppModel { 

 var $name = 'Group'; 

 var $belongsTo = array( 
        'ParentGroup' => 
            array('className' => 'Group', 
                  'foreignKey' => 'parent_id' 
        ), 
     ); 

 var $hasMany = array( 
    'ChildGroup' => 
            array('className' => 'Group', 
                  'foreignKey' => 'parent_id' 
            ), 
    ); 
} 
?>
Thursday, October 6, 2022
 
berkuqo
 
1

What you're after is linking two models together. CakePHP has a solid relationship mapper that does all this for you without needing to "take the id from the last insert and do something else with it".

If I'm not mistaken, your problem put into simple terms is:

An Employee hasMany users

A User belongs to one Employee

For a 1-to-many model relationships CakePHP has something called "hasMany": http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany

It's in the same way a user for instance can make many comments; An employee can create many users.

On the flipside of that relationship between a user and a employee, you'd want a "belongsTo" mapping, so you know that the user was added by some employee: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto

This way you can find what employee that added "user X" by searching "through the user", so to speak.

When you have mapped up the models together, things like retrieving data (like findBy: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#findby) and managing model relationships becomes much easier as you don't have to think much about how relations are chained together (that's what the model mapping is for as outlined above), but rather on what you want to retrieve.

In turn it allows you to do things like this intuitively

$this->Employee->find('all', array(
            'recursive' => 1,
            'fields' => array('employee.name, employee.age')
            ));

or if you wanted to find say what employee that added the user "Sarah".

$this->Employee->User->find('all', array(
'conditions' => array('User.name' => 'Sarah'),
'fields' => 'Employee.*',
'recursive' => 0))

which would give back an associative array with employees.

As a sidenote: The use of recursive = 1 is abit old fashioned. There is something much better called "Containable" that allows you to kind of inject models based on what you need. It's outside the scope of this problem, but I though I'd mention it.

Thursday, December 8, 2022
 
5

1) Go to conf folder in tomcat installation directory

 e.g. C:Tomcat 6.0conf

2) Edit following tag in server.xml file

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

3) Change the port=8080 value to port=80

4) Save file.

5) Stop your Tomcat and restart it.

Saturday, December 24, 2022
4

You can make a list containing all your widgets you want to change

myWidgets = [button1, label1, ... ] # List of widgets to change colour
for wid in myWidgets:
    wid.configure(bg = newColour)

Here's an example code of changing the background colour of multiple labels at once.

import tkinter as tk


# Change all label backgrounds
def change_colour():
    c = user.get() #Get the entered text of the Entry widget
    for wid in widget_list:
        wid.configure(bg = c)

# Create GUI
root = tk.Tk()

tk.Label(root, text='Enter a colour').pack()

user = tk.Entry(root)
user.pack()

label_frame = tk.Frame(root)
label_frame.pack()

btn = tk.Button(root, text='Change Colour', command = change_colour)
btn.pack()

widget_list = [user, btn] # Add defined widgets to list

#Dynamicly create labels for example
for x in range(10): 
    lbl = tk.Label(label_frame, text='Label '+str(x))
    lbl.pack(side = tk.LEFT)
    widget_list.append(lbl) #Add widget object to list

root.mainloop()

Or if you have a Frame already containing all the widgets you want to change, then you can use this instead.

parent_widget.winfo_children() will return a list containing all the widgets stored inside the parent widget

def change_colour():
    c = user.get()
    for wid in label_frame.winfo_children():
        wid.configure(bg = c)
Monday, December 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages