Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp: Adding new attachment without adding a new project
    primarykey
    data
    text
    <p>I'm using the Upload Plugin 2.0 josegonzalez. An image uploader for Cakephp thats works fantastic.</p> <p>But i got a problem, i am able to use the <code>createWithAttachments</code> part of the Plugin. <br /> Everything works well, except for one thing. I dont want to create a new Project (id, name etc) when im uploading new attachments (photo's).</p> <p>A project has more attachements (photo's) it is possible for a user to upload a total of 10 photo's for each project. </p> <p>code from the <code>Project.php</code> <strong>(the model)</strong></p> <pre><code>&lt;?php class Project extends AppModel { /* the rest of your model here */ public function createWithAttachments($data) { // Sanitize your images before adding them $images = array(); if (!empty($data['Image'][0])) { foreach ($data['Image'] as $i =&gt; $image) { if (is_array($data['Image'][$i])) { // Force setting the `model` field to this model $image['model'] = 'Project'; $images[] = $image; } } } $data['Image'] = $images; // Try to save the data using Model::saveAll() $this-&gt;create(); if ($this-&gt;saveAll($data)) { return true; } // Throw an exception for the controller throw new Exception(__("This post could not be saved. Please try again")); } } ?&gt; </code></pre> <p>Code from <code>ProjectsController.php</code> <strong>(the controller)</strong></p> <pre><code>&lt;?php class ProjectsController extends AppController { /* the rest of your controller here */ public function cms_album() { if ($this-&gt;request-&gt;is('post')) { try { $this-&gt;Project-&gt;createWithAttachments($this-&gt;request-&gt;data); $this-&gt;Session-&gt;setFlash(__('The message has been saved')); } catch (Exception $e) { $this-&gt;Session-&gt;setFlash($e-&gt;getMessage()); } } } } ?&gt; </code></pre> <p>Each time i add 10 photo's to the database table <code>attachments</code> it created a new project in the database table <code>projects</code>. I just want the attachments to be new and save the id from the project that i get from the form part <code>echo $this-&gt;Form-&gt;input('Image.'.$i.'.foreign_key', array('type' =&gt; 'hidden', 'value' =&gt; ''.$this-&gt;params-&gt;pass[0].''));</code></p> <p>I hope i wrote my problem clearly and that somebody can help me. I tried a lot of things, even trying to accomplish with an <code>AttachmentsController</code> <em>(with no luck)</em></p> <p><strong>UPDATE:</strong> (After the awnser of <strong>Anil kumar</strong> )</p> <p>This is a <code>print_r</code> on <code>$data</code> before it uses <code>if($this-&gt;saveAll($data))</code></p> <pre><code>Array ( [Image] =&gt; Array ( [0] =&gt; Array ( [model] =&gt; Project [foreign_key] =&gt; 7 [attachment] =&gt; Array ( [name] =&gt; DSCN4923.JPG [type] =&gt; image/jpeg [tmp_name] =&gt; /tmp/phpGbIKTl [error] =&gt; 0 [size] =&gt; 141994 ) ) [1] =&gt; Array ( [model] =&gt; Project [foreign_key] =&gt; 7 [attachment] =&gt; Array ( [name] =&gt; DSCN4921.JPG [type] =&gt; image/jpeg [tmp_name] =&gt; /tmp/phpJBeYxk [error] =&gt; 0 [size] =&gt; 216931 ) ) [2] =&gt; Array ( [model] =&gt; Project [foreign_key] =&gt; 7 [attachment] =&gt; Array ( [name] =&gt; DSCN3810.JPG [type] =&gt; image/jpeg [tmp_name] =&gt; /tmp/phpR6sflk [error] =&gt; 0 [size] =&gt; 1304426 ) ) ) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload