James Dawson
James Dawson

Reputation: 5409

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!

Upvotes: 0

Views: 138

Answers (2)

Chris
Chris

Reputation: 429

You have several solutions to fix this.

The solution provided by pollirata is a very good one. Providing a select with existing categories is the simplest way for an user to know which categories already exist. Having a simple text input leads to spelling mistakes anyway or very similar categories names when you would only want one.

If you want to keep the text input, then you should check for existing categories in your Page.php model in the beforeSave() function: retrieve the categories list and match it against $this->data['Category']['name'] of the data you are trying to save. If an existing category matches, add its id in $this->data['Category']['id] so Cake does not create a new record.

Upvotes: 1

pollirrata
pollirrata

Reputation: 5286

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

Upvotes: 0

Related Questions