Wednesday, November 23, 2011

Re: CakePHP does not save multilanguage attachment

For now i have to set my image field in controller before saving it as
i am hurry with my project.So i have to use another component to
upload file and get filename lik $this->request->data['HowItWork']
['image']=$this->uploadFile->upload(...); like

public function admin_add() {
if ($this->request->is('post')) {
$fileUploaded = $this->Fileupload-
>uploadFiles('img'.'/'.HOW_IT_WORKS_FOLDER, $this->request-
>data['HowItWork']['upload_image'],null,'image');
if(empty($fileUploaded['urls']))
{
$this->request->data['HowItWork']
['upload_image']="";
}else{
$this->request->data['HowItWork']
['upload_image']="dummy";
$this->request->data['HowItWork']['image']=
$fileUploaded['urls'][0];

}
$this->HowItWork->create();
if ($this->HowItWork->save($this->request->data)) {

$this->Session->setFlash(__('The how it work has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The how it work could not be saved.
Please, try again.'),'error_msg');
}
}
}

It works as it should be but I don't understand one thing though.When
in set $this->data['HowItWork']['image'] in beforeSave() function in
my model(like previous post) it doesn't work and doesn't get saved in
i18n table.What am i doing wrong in the AppModel?I still want to use
my old function to upload file from AppModel cause it doesn't require
any coding in my controller and works for others controller as well.So
still looking for solutions

On Nov 24, 10:25 am, msujit <shoes...@gmail.com> wrote:
> Ye as i said earlier i am using a function in beforeSave to upload the
> image and set a variable to that uploaded image name.This was fine
> when there was no i18n table.I used this in my other projects which
> doesn't have multilanguage.But it's not working for i18n table.I am
> using 'upload_image' as file field in my form and then set the
> uploaded filename in 'image' field in table to be stored.Here are my
> function:
>
> In my child HowItWork model:
>  function beforeSave() {
>
>                 return $this-
>
> >upload_image('img/'.HOW_IT_WORKS_FOLDER,'image');
> }
>
> In my parent AppModel:
>
> function upload_image($path,$field='image') {
>             if( isset( $this->data[$this->alias]['upload_image'] ) ) {
>                         $this->file_path=$path;
>                     if(!empty($this->data[$this->alias]['prev_image']) &&
> empty($this->data[$this->alias]['upload_image']['name'])){
>                            $this->data[$this->alias]['upload_image']=$this->data[$this->alias]['prev_image'];
>
>                            return true;
>                     }
>
>                     $this->create_folder($this->file_path);
>                     $filename=$this->check_filename($this->file_path,$this->data[$this->alias]['upload_image']['name']);
>
>                     $this->copy_file($this->data[$this->alias]['upload_image']
> ['tmp_name'], $this->file_path.$filename);
>                     $this->data[$this->alias][$field]=$filename;
>
>                     if(!empty($this->data[$this->alias]['prev_image'])){
>                         $this->delete_file($path.$this->data[$this->alias]
> ['prev_image']);
>                     }
>             }
>             return true;
>         }
>
> On Nov 24, 1:47 am, "Constantin.FF" <constantin...@gmail.com> wrote:
>
>
>
>
>
>
>
> > @msujit do you use a Behavior in your attachment model to set the
> > title and to create thumbs before inserting them in the database or
> > everything is inside the model?
> > I have tested to set a new 'title' field in me form and this title got
> > inside the i18n table.
>
> > On Nov 23, 5:43 pm, Constantin FF <constantin...@gmail.com> wrote:
>
> > > I created new Debugtranslate Behavior just to find the reason of not
> > > saving the attachment title and body in the i18n table
> > > Here is the behavior
>
> > > <?php
> > > App::import('Behavior', 'Translate');
> > > class DebugtranslateBehavior extends TranslateBehavior {
>
> > >     function setup(&$Model, $settings) {
> > >         if (!isset($this->__settings[$Model->alias])) {
> > >             $this->__settings[$Model->alias] = array(
> > >                 'fields' => array(),
> > >             );
> > >         }
> > >         if (!is_array($settings)) {
> > >             $settings = array();
> > >         }
> > >         $this->__settings[$Model->alias] =
> > > array_merge($this->__settings[$Model->alias], $settings);
> > >         //setup Translate behavior with translatable fields
> > >         return parent::setup($Model,
> > > $this->__settings[$Model->alias]['fields']);
> > >     }
> > >     function beforeSave(&$Model) {
> > >                 parent::beforeSave($Model, $created);
> > >         pr($Model);die;
> > >         return true;
> > >     }}
>
> > > ?>
>
> > > If the Debugtranslate behavior is attached to some ordinari model
> > > saving a post I get this inside the $Model array:
>
> > > Node Object
> > > (
> > >     [Behaviors] => BehaviorCollection Object
> > >         (
> > >               [Debugtranslate] => DebugtranslateBehavior Object
> > >                 (
> > >                     [runtime] => Array
> > >                         (
> > >                              [Node] => Array
> > >                                 (
> > >                                     [beforeSave] => Array
> > >                                         (
> > >                                             [title] => test
> > >                                             [body] => test-node
>
> > >                                         )
> > >                                 )
>
> > >                          )
>
> > >                 )
>
> > >         )
> > > )
>
> > > BUT when saving attachment those fields does not exists.
> > > What more can I look for?
> > > On Wed, Nov 23, 2011 at 2:15 PM, DigitalDude
>
> > > <e.blumsten...@googlemail.com> wrote:
> > > > Hey,
>
> > > > @msujlt: you should always 'name' your tanslated fields, but I think
> > > > cake does not force this.
> > > > You then should remove the old field from the table (e.g. table
> > > > attachments, field title).
>
> > > > When you SAVE your racords (add/edit), you have to set the locale for
> > > > your model:
> > > > $this->Attachment->locale = 'eng';
>
> > > > Note: I don't know what locales are defined in your app. Also, there
> > > > can be differences when your locales are build like "en-gb", the
> > > > locale will be "eg_gb".
> > > > Try doing that, it will work but I guess you'll have to test it out
> > > > with the locales of your app...
>
> > > > --
> > > > Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> > > > Check out the new CakePHP Questions sitehttp://ask.cakephp.organdhelpothers with their CakePHP related questions.
>
> > > > To unsubscribe from this group, send email to
> > > > cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: