Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP: saving file with new record with FileUpload plugin
    primarykey
    data
    text
    <p>I'm trying to use the <code>FileUpload</code> plugin (https://github.com/webtechnick/CakePHP-FileUpload-Plugin) in my CakePHP (1.3) app.</p> <p>I have two models: <code>PendingContract</code> and <code>PendingContractFile</code>. A <code>PendingContract</code> can have many <code>PendingContractFile</code> records. When saving a new <code>PendingContract</code>, I'd also like to save the uploaded <code>PendingContractFile</code>; however, my save method fails because <code>PendingContract</code> does not yet have an ID, and that is used as the foreign key in my <code>PendingContractFile</code>.</p> <p>For clarity, here are my models:</p> <pre><code>&lt;?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' =&gt; array( 'className' =&gt; 'PendingContract', 'foreignKey' =&gt; 'pending_contract_id' ), 'Author' =&gt; array( 'className' =&gt; 'User', 'foreignKey' =&gt; 'author_id' ) ); } </code></pre> <p>And here is my controller method where I'm saving my <code>PendingContract</code>:</p> <pre><code>&lt;?php class PendingContractsController extends AppController { function add() { if (!empty($this-&gt;data)) { if ($this-&gt;FileUpload-&gt;success) { $this-&gt;Session-&gt;setFlash('Pending contract successfully created.'); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash($this-&gt;FileUpload-&gt;showErrors()); } } } } </code></pre> <p>Currently the error I'm getting is:</p> <blockquote> <p>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)</p> </blockquote> <p>How can I use the <code>FileUpload</code> plugin so that it attaches the uploaded file with my new <code>PendingContract</code> record?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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