How to update translation cakephp but not main table - cakephp

I have added translate behaviour to a model, the model comes here
App::uses('AppModel', 'Model');
class Category extends AppModel
{
public $hasMany = "Product";
public $validate = array(
'name' => array(
'rule' => 'notEmpty'
)
);
public $actsAs = array(
'Translate' => array(
'name','folder','show'
)
);
public $name = "Category";
public $translateModel = 'KeyTranslate';
}
And heres the controller for updating the model
public function admin_edit_translate($id,$locale)
{
$this->Category->locale = $locale;
$category = $this->Category->findById($id);
if ($this->request->is('post') || $this->request->is('put')) {
$this->Category->id = $id;
if ($this->Category->save($this->request->data)) {
$this->Session->setFlash('Category translate has been updated');
//$this->redirect(array('action' => 'edit',$id));
} else {
$this->Session->setFlash('Unable to update category');
}
}
if (!$this->request->data) {
$this->request->data = $category;
}
}
My Problem is that i have a name field in the categories database and when i update or create a new translation it gets updated with the translated value. How do i avoid that

You must use Model::locale value to set code language for save in database

This happens because the TranslateBehavior uses callbacks like beforeSave and afterSave to save translated content, so it needs to let the model's save operation continue and thus will contain the last translated content.
You could get around this by tricking the TranslateBehavior into thinking the model is saving something by calling the beforeSave and afterSave like this:
$Model = $this->Category;
$Model->create($this->request->data);
$Model->locale = $locale;
$beforeSave = $Model->Behaviors->Translate->beforeSave($Model, array(
array(
'callbacks' => true
)
));
if($beforeSave) {
$Model->id = $id;
$Model->Behaviors->Translate->afterSave($Model, true);
}
This way the translation will be saved and the main table will be left untouched. Might not be the best way to save translations though. Why do you need to leave the main table untouched?

Callback Behavior::beforeSave is before Model::beforeSave...
but, the simplest way to modify data in Model::beforeSave before Behavior::beforeSave before realy saving is:
$this->Behaviors->Behavior_Name->runtime[Model_Name]['beforeSave'][Field_Name] = '...';

Related

Cakephp editing BelongsTo associated field

I'm having problem in editing associated (BelongsTo) field...
Class ModelA extends AppModel {
public $belongsTo = array ('ModelB');
public $actsAs = array('Containable');
public function getModelA($id){
$modelA = $this->find('all', array('contain' => array('ModelB'),'conditions' => array('ModelA.id' => $id)));
if(count($modelA)>0){
$modelA = $modelA[0];
}
return $modelA;
}
}
class ModelAsController extends AppController {
public function edit($id = null) {
$modelA = $this->ModelA->getModelA($id);
$this->set('modelA', $modelA);
}
}
Data in the $modelA is in the format:
array(
'ModelA' => array(
...
),
'ModelB' => array(
...
)
)
So far, everything is fine...
But, I'm stuck with the edit.ctp. I need input for editing of the field1 from the ModelB, but I'm getting empty input box. Line of the code goes like this:
echo $this->Form->input('ModelB.field1');
When I try to debug:
Debugger::log($modelA['ModelB']['field1']);
then I have correct value.
What would be the way to have input box filled with field1 of the ModelB?
for a FormHelper to pickup the model data you have to set
$this->request->data = $modelA;
in the Controller, otherwise the helper has no idea where you've hidden the data :)
(tip: usually you can debug the helper code in such cases)

CakePHP: finding data after save

I'm working on an edit method. After saving data, an email is sent out with the changes made during the edit. Everything works except for one infuriating but crucial bug. Here it is distilled down very simply:
$data = $this->SupportTicket->readForView($st_id);
$this->SupportTicket->id = $st_id;
if ($this->SupportTicket->save($this->request->data)) {
//call custom model method to pull view data
$data = $this->SupportTicket->readForView($st_id);
//do something with this data
}
The issue is that $data comes out with the pre-save data. So what I then try to do with the new data doesn't work.
I can't just use $this->request->data because it doesn't have the full data that I want in it.
The save does however work. If I refresh the view method for the same record, it shows as updated. So it's saving, but when I do the find after saving it is giving me old data.
Any ideas?
Update: it doesn't happen with findById($st_id) so it must be something to do with my custom method. Code:
public function readForView($id)
{
$data = $this->find('first', array(
'conditions' => array(
'SupportTicket.id' => $id
),
'contain' => array(
'User',
'Owner'
)
));
if (empty($data)) {
throw new notFoundException('Ticket not found');
}
$data['SupportTicket']['type_long'] = $this->getLongType($data['SupportTicket']['type']);
$data['SupportTicket']['status_long'] = $this->getLongStatus($data['SupportTicket']['status']);
$data['SupportTicket']['name'] = 'Support Ticket #' . $data['SupportTicket']['id'] . ' - ' . $data['SupportTicket']['title'];
return $data;
}
Copying the code from this method into the Controller gives the same result.
I've found this helpful: https://edivad.wordpress.com/2008/04/15/cakephp-disable-model-queries-caching/
By model:
class Project extends AppModel {
var $cacheQueries = false;
...
By function:
function someFunction {
$this->Model->cacheQueries = false;
...
try using last Insert ID
$id=$this->getLastInsertID();
public function readForView($id)

CakePHP hasOne-belongsTo relationship, how to look up and insert foreign key from user input?

I have two tables:
Property (..., postcode_id, ...)
Postcode (id, postcode, latitude, longitude)
The relationship:
class Property extends AppModel {
public $belongsTo = array(
'Postcode' => array(
'className' => 'Postcode'
)
);
class Postcode extends AppModel {
public $hasMany = array(
'Property' => array(
'className' => 'Property'
)
);
What I'd like to happen:
Enter a postcode in the Property add form.
Check entered postcode is in the Postcode table.
If not, so some logic (possibly add it, just return validation error for now).
If so, then record the Postcode.id in Properties.postcode_id (the foreign key).
I can't figure out the best way to do this with cake. A custom Property validation function that does the check and adds it to data to be added? Or in beforeValidation? Or does Cake deal with this?
Thanks!
EDIT
Based on the answer, I think this is the best solution...
I think this is the better way to do it, as it validates too. Thanks Anubhav for the putting me on the right track!
As per Anubhav's answer, in the Postcode model:
public function checkPostcode($postcode = null){
$record = $this->find('first', array(
'conditions'=>array('postcode' => $postcode),
'fields' => array('id')
));
if (!empty($record)){
return $record['Postcode']['id'];
} else {
return false;
// Logic for finding and adding new postcode
}
}
But and in the Property model:
public $validate = array(
'postcode' => array(
'exists' => array(
'rule' => 'postcodeExists',
'message' => 'Postcode does not exist'
)
)
);
public function postcodeExists($check) {
$id = $this->Postcode->checkPostcode($check);
if ($id) {
$this->data['Property']['postcode_id'] = $id;
return true;
}
return false;
}
public function beforeSave($options = array()) {
unset($this->data['Property']['postcode']);
}
By modifying the values to save here, the controller is kept skinny, and, rather than making sure postcode is real then finding the id, both are done at once, so only one query is needed.
Follow the steps:
Step 1: Create one action in Postcode.php
function checkPostcode($postcodeValue = null){
$returnData = $this->find('first',
array('conditions'=>array('postcode'=>$postcodeValue),
'fields'=>array('id'))
);
if(!empty($returnData)){
return $returnData['Postcode']['id'];
}else{
$saveData['Postcode']['postcode'] = $postcodeValue;
$this->save($saveData);
return $this->getLastInsertID();
}
}
The above function will check postcode from postcode model and insert postcode if not exist and returns the id of postcode table for corresponding row.
Step 2: Call this function controller action as
function someFuntion(){
$postcode_id = $this->Postcode->checkPostcode($userInputPostcodeHere);
// Write code to save property here
}
Hope this will give you some idea about the logic...

Cakephp: Associations not working

I want to do a simple cakephp association program but it's not working.
I have two database tables: users and sec_datas. When i run this program it just shows the result of first row of users table, not the result of both tables which have same sec_id value.
Controller code:
<?php
class UsersController extends AppController
{
public function index()
{
$this->autoRender = FALSE;
$this->loadModel('User');
$storeDivisions = $this->User->find();
echo "<pre>";
print_r($storeDivisions);
echo "</pre>";
}
}
Model code:
<?php
class User extends AppModel {
public $useTable='users';
public $hasOne = array(
'Sec_data' => array(
'ClassName' => 'Sec_data',
'Conditions' => array('User.sec_id=Sec_data.sec_id'),
'Dependent' => false
)
);
}
?>
If the primary key of User model is id then cake try to associate the foreign key in Sec_data with that key regardless the conditions you set.
First of all you should do domething like
public $hasOne = array(
'Sec_data' => array(
'ClassName' => 'Sec_data',
'ForeignKey' => 'sec_id',
'Dependent' => false
)
);
But it would work only if sec_id is related to User.id
if you want Sec_data.sec_id related to User.sec_id (and User.sec_id is different from User.id) then you have to join the tables manually
edit: see comments
There are a couple of things going wrong here. You should read up on CakePHP's conventions.
Your associated model class should be called SecData, then it will automatically map to the sec_datas table.
No need to use loadModel.
No need to specify conditions. Set the correct recursive level instead.
For hasOne, only one table has a foreign key. By default, the other Model has it ("User hasOne SecData" -> SecData has foreign key). So in your example, you should remove the column sec_id from User, and add a user_id column to your SecData Model.
See also: CakePHP Book on associations
Updated Controller code:
<?php
class UsersController extends AppController {
public $uses = array('User'); // Do this instead of loadModel
public function index() {
$this->autoRender = FALSE;
$this->User->recursive = 1; // Make User load associated records
$storeDivisions = $this->User->find('all');
pr($storeDivisions); // pr() is a Cake shorthand for print_r wrapped in <pre>
}
}
For your model:
<?php
class User extends AppModel {
// Unecessary. Convention is to use lowercase classname + 's', which
// already gives us 'users'
// public $useTable='users';
public $hasOne = array(
'SecData' => array( // Model class Sec_Data must be renamed accordingly
'dependent' => false
)
);
}
?>
Study model association in cake php . Here is very good explanation http://www.phpsupercoder.com/model-association-cake-php

CakePHP: saving file with new record with FileUpload plugin

I'm trying to use the FileUpload plugin (https://github.com/webtechnick/CakePHP-FileUpload-Plugin) in my CakePHP (1.3) app.
I have two models: PendingContract and PendingContractFile. A PendingContract can have many PendingContractFile records. When saving a new PendingContract, I'd also like to save the uploaded PendingContractFile; however, my save method fails because PendingContract does not yet have an ID, and that is used as the foreign key in my PendingContractFile.
For clarity, here are my models:
<?php
class PendingContract extends AppModel {
var $name = 'PendingContract';
var $belongsTo = array(
'Supplier'
);
var $hasMany = array(
'PendingContractFile'
);
}
class PendingContractFile extends AppModel {
var $name = 'PendingContractFile';
var $belongsTo = array(
'PendingContract' => array(
'className' => 'PendingContract',
'foreignKey' => 'pending_contract_id'
),
'Author' => array(
'className' => 'User',
'foreignKey' => 'author_id'
)
);
}
And here is my controller method where I'm saving my PendingContract:
<?php
class PendingContractsController extends AppController {
function add() {
if (!empty($this->data)) {
if ($this->FileUpload->success) {
$this->Session->setFlash('Pending contract successfully created.');
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash($this->FileUpload->showErrors());
}
}
}
}
Currently the error I'm getting is:
1452: Cannot add or update a child row: a foreign key constraint fails (pending_contract_files, CONSTRAINT pending_contract_files_ibfk_1 FOREIGN KEY (pending_contract_id) REFERENCES pending_contracts (id) ON DELETE CASCADE ON UPDATE CASCADE)
How can I use the FileUpload plugin so that it attaches the uploaded file with my new PendingContract record?
I took a look at the plugin, at it doesn't appear that it will save posted data along with uploaded files. It purposefully separates upload file data from any other input in the form and performs a save for each file.
Personally I would try other plugins such as https://github.com/josegonzalez/upload which do not rely in any controller-level code.
public function beforeSave($options = array()) {
if (!isset($this->data[$this->alias][$this->primaryKey])) {
$this->data[$this->alias][$this->primaryKey] = String::uuid();
}
return parent::beforeSave($options);
}
This will generate a new UUID for the record before save. You should probably only do this if the key is not already set.
i had a similar problem, what i did is unset the validation when adding a new PendingContractFile in your case. So before the saveAll method try adding:
unset($this->PendingContract->PendingContractFile->validate['pending_contract_id']);
so it wont check for the foreign_key.
hope it helps.

Resources