Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp saving related model data creating blank record with one field
    primarykey
    data
    text
    <p>I've been trying to figure this problem out all morning. Tried some things from other questions, but it either didn't really apply to my situation or didn't work. I have two tables:</p> <p><strong>users</strong> = (id,name,username,password,roles,last_edit,language)<br> <strong>french_translations</strong> = (id, french_clinical_recommendations, french_tradenames, include_drug, french_category, drug_id, user_id)</p> <p>User hasMany french_translations and french_translations belongsTo User. </p> <p>When a user adds or edits a french_translation I want it to save the user id in the french_translations table, the field user_id, for that record, and then put the generic name for a drug in the last_edit field in the users table. Right now it creates a new record in each table. In the users table, everything is blank except for the id and last_edit field(Which puts the correct drug name in the field). And the french_translations table has a record with blanks, with the user_id being the same as the blank one created in the users table.</p> <p>Controller: </p> <pre><code> function add($id = null) { $userid = $session-&gt;read('Auth.User.id'); $drug = $this-&gt;FrenchTranslation-&gt;Drug-&gt;read( array( 'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations', 'Drug.category','Drug.lrc' ), $id ); $this-&gt;set('user',$userid); $this-&gt;set('drug',$drug); if (!empty($this-&gt;data)) { $french_translation['FrenchTranslation']['id'] = $this-&gt;Session-&gt;read('id'); $this-&gt;FrenchTranslation-&gt;create(); if ($this-&gt;FrenchTranslation-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The french translation has been saved', true)); $this-&gt;redirect(array('controller'=&gt;'drugs','action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The french translation could not be saved. Please, try again.', true)); } } $drugs = $this-&gt;FrenchTranslation-&gt;Drug-&gt;find('list'); $this-&gt;set(compact('drugs')); } function edit($id = null) { //$this-&gt;FrenchTranslation-&gt;id = $id; $userid = $this-&gt;Auth-&gt;user('id'); $username = $this-&gt;Auth-&gt;user('name'); //$this-&gt;FrenchTranslation-&gt;user_id = $id; $drug = $this-&gt;FrenchTranslation-&gt;Drug-&gt;read( array( 'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations', 'Drug.category','Drug.lrc' ), $id ); $this-&gt;set('drug',$drug); $this-&gt;set('user',$userid); $this-&gt;set('username',$username); if (!$id &amp;&amp; empty($this-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('Invalid french translation', true)); $this-&gt;redirect(array('action' =&gt; 'index')); } if (!empty($this-&gt;data)) { if ($this-&gt;FrenchTranslation-&gt;saveAll($this-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The french translation has been saved', true)); $this-&gt;redirect(array('controller'=&gt;'drugs','action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The french translation could not be saved. Please, try again.', true)); } } if (empty($this-&gt;data)) { $this-&gt;data = $this-&gt;FrenchTranslation-&gt;read(null, $id); } $drugs = $this-&gt;FrenchTranslation-&gt;Drug-&gt;find('list'); $this-&gt;set(compact('drugs')); } function delete($id = null) { if (!$id) { $this-&gt;Session-&gt;setFlash(__('Invalid id for french translation', true)); $this-&gt;redirect(array('action'=&gt;'index')); } if ($this-&gt;FrenchTranslation-&gt;delete($id)) { $this-&gt;Session-&gt;setFlash(__('French translation deleted', true)); $this-&gt;redirect(array('action'=&gt;'index')); } $this-&gt;Session-&gt;setFlash(__('French translation was not deleted', true)); $this-&gt;redirect(array('action' =&gt; 'index')); } </code></pre> <p>Edit View: </p> <pre><code> &lt;?php echo $this-&gt;Form-&gt;input('User.last_edit',array('type'=&gt;'hidden','value'=&gt;$drug['Drug']['generic'])); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('user_id', array('type'=&gt;'hidden','value'=&gt;$user)); ?&gt; </code></pre>
    singulars
    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