Viewed   81 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
 
1

You could also modify that model's aftersave method so that it returns $data['Modelname'] after it performs the query... In essence, it'd basically be an array shift and you would only have $arrayname['fieldname'] rather than $arrayname['Model']['fieldname']. Is that what you were asking?

Wednesday, December 14, 2022
 
5

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')

Thursday, December 15, 2022
 
4

I would start at your model definitions: Your has_many association needs to have an underscore in the name - SubProject -> sub_project:

class Project < ActiveRecord::Base
 attr_accessible :name, :description
 has_many :sub_projects # !
end

Next, your belongs_to needs to be in a singular form:

class SubProject < ActiveRecord::Base
  attr_accessible :id_name, :description, :num_alloc, :project_id
  belongs_to :project # !
end

After you make these changes, you can query:

@sub_projects = SubProject.includes(:project).all
name = @sub_projects.first.project.name
...
Friday, September 2, 2022
 
3

you simply have to store the Strings.xml in a folder with the countrycode eg one Strings.xml in values-en one in values-fr and one in values-de. The app will automatically pic the folder with the right country-code according to the phone language

Tuesday, December 20, 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 :