开发者

CakePHP - add item to 2 models

How can I add an item and insert it into 2 tables?

So I have a 'Type' table and 'SpecificType' table.

Type 开发者_开发技巧has fields 'id' and some other common fields.

SpecificType has fields 'id', 'type_id' and some other uncommon fields.

When I go to /specific_types/add and I submit, ideally I want to first add that to 'Type' then add it to 'SpecificType'.

This is what I have now.

In SpecificType Model

var $belongsTo = array(
    'Type' => array(
        'className' => 'Type',
        'foreignKey' => 'type_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

In SpecificType Controller

var $uses = ('Type', 'SpecificType');

function add() {
    if (!empty($this->data)) {
        $this->Type->create();
        if ($this->Type->save($this->data)) {
            $this->SpecificType->create();
            if ($this->SpecificType->save($this->data)) {
                $this->Session->setFlash(__('The SpecificType has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The SpecificType could not be saved. Please, try again.', true));
            }
        } else {
            $this->Session->setFlash(__('The Type could not be saved. Please, try again.', true));
        }
    }
}

In SpecificType add.ctp

echo $form->input('Type.data1');
echo $form->input('title');

So right now, it saves Type.data1 but title isn't getting saved. What am I missing?

Thanks,

Tee

Additional Info: The 2nd model isn't saving only when I turn on MeioUpload.


Make sure your view is setup to create a form for SpecificType:

<?php echo $this->Form->create('SpecificType', array('action' => 'add')); ?>
<?php echo $this->Form->input('data1'); ?>
<?php echo $this->Form->input('title'); ?>
<?php echo $this->Form->end(); ?>

This will put all of your form data into: $this->data['SpecificType']

Right before your code:

$this->Type->create();

You need to do this:

$this->data['Type'] = $this->date['SpecificType'];

Then process the save. As long as the view is setup correctly for the SpecificType controller, all of the data from the form will be stored in $this->data['SpecificType']. If you pr($this->data) and there is data you need to save outside of $this->data['SpecificType'], review and fix the view.

Side note: Your design sounds awfully sketchy. You should never need to save the data in two locations. I would recommend revisiting the design of your app. There is something fundamentally wrong with it if you need to save the same data into two tables.


Just copy the data over to a SpecificType index in the data array and use saveAll(). The models are related so Cake will automatically link them via the type_id. We're working off the Type alias, so make sure you also have var $hasMany = array('SpecificType'); in your Type model.

function add() {
    if (!empty($this->data)) {
        $this->Type->create();
        $this->data['SpecificType'] = $this->data;
        if ($this->Type->saveAll($this->data)) {
            $this->Session->setFlash(__('The SpecificType has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The SpecificType could not be saved. Please, try again.', true));
        }
    } else {
        $this->Session->setFlash(__('The Type could not be saved. Please, try again.', true));
    }
}


The code seems to be fine, use saveAll instead of save

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜