Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to copy one Model1 instance to Model2 instance ? #79

Open
prigal opened this issue Jun 14, 2014 · 6 comments
Open

How to copy one Model1 instance to Model2 instance ? #79

prigal opened this issue Jun 14, 2014 · 6 comments

Comments

@prigal
Copy link

prigal commented Jun 14, 2014

Hi,

How can I copy $model1->image to $model2->image ?

I tryied :

$model2->image=$model1->image->path();
$model2->save();

It succeed but it delete model2 original file, how can I preserve it ?

Thanks,

@tabennett
Copy link
Contributor

Set the keep_old_files flag to true on model2. This will prevent old file uploads from being deleted when you upload a new file.

@prigal
Copy link
Author

prigal commented Jun 18, 2014

Yes, I knew I can do it, but that's not what I want in fact.

I don't want to keep_old_files on model2 because I want them to be deleted
and replaced on new uploads, I just don't want them to be removed if there
are "copied"...

I use a workaroud : copying the file with 'copy' before assigning it to
model2... The copy is deleted so the model1 is kept in place.

2014-06-17 15:53 GMT+02:00 Travis Bennett [email protected]:

Set the keep_old_files flag to true on model2. This will prevent old file
uploads from being deleted when you upload a new file.


Reply to this email directly or view it on GitHub
#79 (comment).

@tabennett
Copy link
Contributor

You can still use keep_old_files, just override the config temporarily:

// Temporarily change the attachment config for the given request:
$model2->image->keep_old_files = true;

// Copy the files over
$model2->image=$model1->image->path();
$model2->save();

// If necessary, change the config back once you're done:
$model2->image->keep_old_files = false;

@prigal
Copy link
Author

prigal commented Jun 18, 2014

Great ! Many thanks.

2014-06-18 21:12 GMT+02:00 Travis Bennett [email protected]:

You can still use keep_old_files, just override the config temporarily:

// Temporarily change the attachment config for the given request:$model2->image->keep_old_files = true;
// Copy the files over$model2->image=$model1->image->path();$model2->save();
// If necessary, change the config back once you're done:$model2->image->keep_old_files = false;


Reply to this email directly or view it on GitHub
#79 (comment).

@prigal
Copy link
Author

prigal commented Jun 18, 2014

Ooops, finaly that's not working...

2014-06-18 22:49 GMT+02:00 Pierre Rigal

Great ! Many thanks.

2014-06-18 21:12 GMT+02:00 Travis Bennett [email protected]:

You can still use keep_old_files, just override the config temporarily:

// Temporarily change the attachment config for the given request:$model2->image->keep_old_files = true;
// Copy the files over$model2->image=$model1->image->path();$model2->save();
// If necessary, change the config back once you're done:$model2->image->keep_old_files = false;


Reply to this email directly or view it on GitHub
#79 (comment).

@dieterve
Copy link

dieterve commented Aug 6, 2014

Als having a similar issues when trying to copy an existing file to a new model. When trying the method above I get an Exception: The file "/vagrant/public/files/original/missing.png" does not exist. Even tho the source file exists.

I also tried this:

$file->file->keep_old_files = true;
$file->file->setUploadedFile($originalFile->file->path());
$file->save();

However it keeps deleting the original file. I have no clue where it happens, did some debugging inside Attachment::clear but the method is never called, yet the file is deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants