Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:36:04+00:00 2026-06-13T23:36:04+00:00

I’m using CakePHP’s translatable behavior. I have a few existing fields working fine, but

  • 0

I’m using CakePHP’s translatable behavior. I have a few existing fields working fine, but I’m having trouble adding a new translatable field to my model.

CakePHP uses an INNER JOIN to fetch all translatable fields from the database.

Now, if I add an extra translatable field to my model, all the translation records for that field won’t exist in the database. And because of the inner join, whenever it tries to fetch ANY existing records from the database, it will return blank – because the INNER JOIN on the new field fails, and so the entire query returns nothing.

Surely people must have come accross this situation before. Is there an easy solution?

One solution would be to edit/override the core and make all the INNER JOIN’s into LEFT OUTER JOIN’s. Is there anything wrong with that?

Another solution would be to run an update on the translations table to create all the extra records for the new field, every time you add a new translatable field – but I hate that solution.

Is there a better solution? How have others dealt with this problem?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:36:06+00:00Added an answer on June 13, 2026 at 11:36 pm

    OK, here’s a way of making sure the records exist after each time you add a new translatable field. If you’ve got a better answer, add it, and I’ll mark yours as correct.

    PS – this is tested for my purposes. I’m using multiple translation tables (http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html#multiple-translation-tables). I think it should work for most situations, but if not, it should at least be a good starting point.

    In your model (the model that actsAs Translatable), add the following method. What it does is takes an array of locales, and then for every record in the table, and for every translatable field, and for every locale (ie, 3 loops), it checks that a translation record exists. If a translation doesn’t exist, it adds a blank one, so at least the INNER JOIN won’t fail.

    It returns an array of all the records it added, so you can then go through and check them or change their content or whatever.

    Here’s the model method:

    function ensureTranslationIntegrity($localesToCheck){
        $allRows = $this->find('all', array('fields' => array('id')));
        $fieldsToCheck = array();
    
        $translatableFields = $this->actsAs['Translate'];
        foreach($translatableFields as $key => $value){
            // actsAs Translatabe can take field names only, or Key => Value pairs - see http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html#retrieve-all-translation-records-for-a-field
            if(is_numeric($key)){
                $field = $value;
            } else {
                $field = $key;
            }
            array_push($fieldsToCheck, $field);
        }
    
        $translateModel = $this->translateModel();
        $addedRows = array(); // This will contain all the rows we have to add
    
        foreach ($allRows as $row){
            foreach($fieldsToCheck as $field){
                foreach($localesToCheck as $locale){
                    $conditions = array(
                        'model' => $this->name,
                        'foreign_key' => $row[$this->name]['id'],
                        'field' => $field,
                        'locale' => $locale
                    );
                    $translation = $translateModel->find('first',array('conditions' => $conditions));
    
                    if(!$translation){
                        $data = $conditions; // The data we want to insert will mostly just match the conditions of the failed find
                        $data['content'] = ''; // add it as empty
    
                        $translateModel->create();
                        $translateModel->save($data);
    
                        array_push($addedRows, $data);
                    }
                } // END foreach($localesToCheck as $locale){
            } // END foreach($fieldsToCheck as $field){
        } // END foreach ($allRows as $row){
    
        return $addedRows;
    }
    

    And in your controller, you’d call it something like this:

    public function ensure_translation_integrity(){
        $locales = array('en_au','en_gb','en_nz','pt_br','xh_za');
        $addedRows = $this->YourModel->ensureTranslationIntegrity($locales);
        debug($addedRows);
    }
    

    Hope that helps someone, but like I said, I’d love to see a better solution if someone has one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.