PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How to keep HABTM with TranslateBehaviour in CakePHP

 April 22, 2022     cakephp, cakephp-2.3, translate     No comments   

Issue

I have a model "Report" where some fields are translatable through TranslateBehaviour. Each Report "Has And Belongs To Many" ReportTags and ReportCategories.

In my ReportsController, I try to add a new Report to DB with:

public function admin_add() {
if ($this -> request -> is('post')) {
    $this -> Report -> create();
    if ($this -> Report -> saveMany($this -> request -> data)) {
        $this -> Session -> setFlash(__('Report saved OK'), 'flash/backend/success');
        return $this -> redirect(array('action' => 'index'));
    } else {
        $this->Session->setFlash(__('Report saved KO. Please, try again'), 'flash/backend/error');
    }
}
$users = $this->Report->User->find('list');
$reportCategories = $this->Report->ReportCategory->find('list');
$reportTags = $this->Report->ReportTag->find('list');
$this->set(compact('users', 'reportCategories', 'reportTags'));
$this->set('title',__('New report'));
$this->set('item_selected',4);
}

But doing this I add three reports to Reports table, the one I want to add and two more empty. i18n table adds the correct rows for translations (one per locale and translatable field), but also the rows with fields for the two empty models added, with "content" field empty.

How can I add only the right values? I tried also to do

unset($this->request->data['ReportCategory']);
unset($this->request->data['ReportTag']);

before saveMany, but this eliminates associations between Report, ReportCategory and ReportTag.

For more info, $this -> request -> data contains:

array(3) { 
["Report"]=> array(11) { 
    ["published"]=> string(1) "0" 
    ["sticky"]=> string(1) "0" 
    ["in_rss"]=> string(1) "1" 
    ["slug"]=> string(4) "asdf" 
    ["media_uri"]=> string(4) "ssss" 
    ["title"]=> array(3) {  
        ["spa"]=> string(3) "www" 
        ["eng"]=> string(3) "eee" 
    } 
    ["summary"]=> array(3) { 
        ["spa"]=> string(3) "www" 
        ["eng"]=> string(3) "eee" 
    } 
    ["body"]=> array(3) { 
        ["spa"]=> string(12) "www" 
        ["eng"]=> string(12) "eee" 
        } 
    } 
["ReportCategory"]=> array(1) { 
    ["ReportCategory"]=> array(1) { 
        [0]=> string(1) "7" 
    } 
} 
["ReportTag"]=> array(1) { 
    ["ReportTag"]=> array(2) { 
        [0]=> string(2) "23" 
        [1]=> string(2) "24" 
    } 
} 
}

Solution

Solved. Problem was saveMany. It must be replaced by saveAll. I use saveMany in other controllers when there are HasMany associations, to save the array of translations for translatable fields. In HABTM associations, this does not work. Hope this helps to somebody.



Answered By - anuf
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing