Note that there are some explanatory texts on larger screens.

plurals
  1. POhasOne relationship saving new record instead of using existing one
    text
    copied!<p>I have a <code>Page</code> model and a <code>Category</code> model. Basically, each page belongs to a category. The models are set up so <code>Page belongsTo Category</code>, and <code>Category hasMany Page</code>.</p> <p>When adding new pages, I'm using this controller action:</p> <pre><code>public function admin_add() { if($this-&gt;request-&gt;is('post')) { $this-&gt;Page-&gt;create(); if($this-&gt;Page-&gt;saveAssocated($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash('The page has been created.'); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash('Unable to save the page.'); } } } </code></pre> <p>and this is the view for that action:</p> <pre><code>&lt;h1&gt;Add Page&lt;/h1&gt; &lt;?php echo $this-&gt;Form-&gt;create('Page'); echo $this-&gt;Form-&gt;input('Page.title', array('required' =&gt; 'required')); echo $this-&gt;Form-&gt;input('Page.body', array('type' =&gt; 'textarea', 'class' =&gt; 'redactor', 'required' =&gt; 'required')); echo $this-&gt;Form-&gt;input('Category.name'); echo $this-&gt;Form-&gt;input('Page.in_header'); echo $this-&gt;Form-&gt;end('Save Page'); ?&gt; </code></pre> <p>Now, when I save a page with "Services" as the category, it all gets saved correctly. The page gets saved and the category with <code>name</code> of "Services" gets inserted and they both get linked up properly.</p> <p>However, when I add another page with the same category of "Services", instead of using the existing record in the <code>categories</code> table Cake makes a new one and links that one instead. I need it to use the existing record.</p> <p>How can I fix my view/controller/database to make this work properly?</p> <p>Thanks!</p>
 

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