Asked  7 Months ago    Answers:  5   Viewed   42 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

55

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'
              )
          )
     )
));
Wednesday, March 31, 2021
 
Wilk
answered 7 Months ago
53

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' 
            ), 
    ); 
} 
?>
Saturday, May 29, 2021
 
ritch
answered 5 Months ago
41

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.

Saturday, May 29, 2021
 
Anand
answered 5 Months ago
17
override func viewDidLoad() {
    super.viewDidLoad()

    let container = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 22))

    let searchBar = UISearchBar()
    searchBar.translatesAutoresizingMaskIntoConstraints = false
    container.addSubview(searchBar)

    let leftButtonWidth: CGFloat = 35 // left padding
    let rightButtonWidth: CGFloat = 75 // right padding
    let width = view.frame.width - leftButtonWidth - rightButtonWidth
    let offset = (rightButtonWidth - leftButtonWidth) / 2

    NSLayoutConstraint.activate([
        searchBar.topAnchor.constraint(equalTo: container.topAnchor),
        searchBar.bottomAnchor.constraint(equalTo: container.bottomAnchor),
        searchBar.centerXAnchor.constraint(equalTo: container.centerXAnchor, constant: -offset),
        searchBar.widthAnchor.constraint(equalToConstant: width)
    ])


    self.navigationItem.titleView = container
}

enter image description here

Thursday, August 12, 2021
 
rampy
answered 3 Months ago
15

Change the order in which the plots are added to the figure, and then call legend normally. That should do it.


You can also do it as follows. First get handles to the individual plots:

h1 = plot(1:5);
hold on
h2 = plot(11:15, 'r');

Then call legend specifying the order:

legend([h1 h2],'plot1','plot2')

or

legend([h2 h1],'plot2','plot1')

enter image description here

enter image description here

Friday, August 27, 2021
 
Hauke Ingmar Schmidt
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :