Search code examples
phpcakephpmodels

hasOne relationship saving new record instead of using existing one


I have a Page model and a Category model. Basically, each page belongs to a category. The models are set up so Page belongsTo Category, and Category hasMany Page.

When adding new pages, I'm using this controller action:

public function admin_add() {
    if($this->request->is('post')) {
        $this->Page->create();
        if($this->Page->saveAssocated($this->request->data)) {
            $this->Session->setFlash('The page has been created.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to save the page.');
        }
    }
}

and this is the view for that action:

<h1>Add Page</h1>
<?php
    echo $this->Form->create('Page');
    echo $this->Form->input('Page.title', array('required' => 'required'));
    echo $this->Form->input('Page.body', array('type' => 'textarea', 'class' => 'redactor', 'required' => 'required'));
    echo $this->Form->input('Category.name');
    echo $this->Form->input('Page.in_header');
    echo $this->Form->end('Save Page');
?>

Now, when I save a page with "Services" as the category, it all gets saved correctly. The page gets saved and the category with name of "Services" gets inserted and they both get linked up properly.

However, when I add another page with the same category of "Services", instead of using the existing record in the categories table Cake makes a new one and links that one instead. I need it to use the existing record.

How can I fix my view/controller/database to make this work properly?

Thanks!


Solution

  • When creating the records, there's no place where you specify the id of the existing Category. By that, Cake assumes that it is a new record, since there's no ID to use to search.

    You need to provide that id of that category in order to help Cake understanding that the record already exists

    Hope it helps