开发者

How to setup edit and add views with a hasOne relationship in cakePHP?

I was wondering if someone could help me out.

I am using cakePHP 1.3 and I am having trouble getting the edit view to update the main Model and a hasOne related Model. I am fairly positive this has to do with my setup of the edit.ctp view. I am using the media plugin which I got working on another model, so I don't believe that has anything to do with that. Specifically I am working on getting the Media Plugin, Monolithic Attachment Model with a hasOne relationship working.

I have checked out the cake docs

http://book.cakephp.org/#!/view/1032/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

read the majority of the docs in the Media Plugin this like is the most relevant

https://github.com/davidpersson/media/blob/next/docs/RECIPES

and spent extensive time searching google.

Any help would be appreciated.

Thanks,

James

Model - client.php

<?php
class Client extends AppModel {
var $name = 'Client';
var $displayField = 'name';
var $actsAs = array(
    'Media.Transfer',
    'Media.Coupler',
    'Media.Generator'
);

[...]
var $hasOne = array(
  'Logo' => array(
      'className' => 'Attachment',
      'foreignKey' => 'foreign_key',
      'conditions' => array('Logo.model' => 'Client'),
      'dependent' => true,
));
[...]
?>

Controller - clients_controller.php

<?php
class ClientsController extends AppController {

var $name = 'Clients';
    [...]
function edit($id = null) {
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid client', true));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        if ($this->Client->saveAll($this->data, array('validate'=>'first') )) {
            $this->Session->setFlash(__('The client has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The client could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->Client->recursive = 0;
        $this->data = $this->Client->read(null, $id);
    }
    $statuses = $this->Client->Status->find('list');
    $this->set(compact('statuses'));
}
    [...]
    ?>

View - edit.ctp

<h1><?php __('Edit Clients');?></h1>
<div class="clients form">
<?php echo $this->Form->create('Client', array('type' => 'file'))."\n";?>
<fieldset>
<?php
   echo $this->Form->input('Client.id')."\n";
   echo $this->Form->input('Client.name')."\n";
   echo $this->Form->input('Client.address1')."\n";
   echo $this->Form->input('Client.address2')."\n";
   [...]
   echo $form->input('Logo.id')."\n";
   echo $form->input('Logo.file', array('type' => 'file'))."\n";
   echo $form->hidden('Logo.foreign_key')."\n";
   echo $form->hidden('Logo.model', array('value' => 'Client'))."\n";
    ?>
</fieldset>

<?php echo $this->Form->end(__('Submit', true));?>
</div>

clients sql

CREATE TABLE `clients` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `address1` varchar(255) NOT NULL,
  `开发者_Python百科address2` varchar(255) NOT NULL,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY  (`id`)
)

attachments sql

CREATE TABLE `attachments` (
  `id` int(10) NOT NULL auto_increment,
  `model` varchar(255) NOT NULL,
  `foreign_key` int(10) NOT NULL,
  `dirname` varchar(255) default NULL,
  `basename` varchar(255) NOT NULL,
  `checksum` varchar(255) NOT NULL,
  `group` varchar(255) default NULL,
  `alternative` varchar(50) default NULL,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY  (`id`)
) 


    var $hasOne = array(
  'Logo' => array(
      'className' => 'Attachment',
      'foreignKey' => 'foreign_key', // 'foreign_key' ? is that a name for your fk?
      'conditions' => array('Logo.model' => 'Client'),
      'dependent' => true,
));

here you haven't defined the foreign_key which binds your logo to your client. look up in your database for the foreign key and put it's name here.


Thanks for all the responses. I finally got it working. It appears that for some reason the saveAll was causing me grief. When I saved the Client then saved the Logo everything worked.

I posted the code below.

Controller Code

function edit($id = null) {
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid client', true));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        $client = $this->Client->save($this->data);
        if (!empty($client)) {            
            $this->data['Logo']['foreign_key'] = $this->Client->id;
            $this->data['Logo']['model'] = 'Client';
            $this->Session->setFlash(__('The client has been saved', true));
            $this->Client->Logo->save($this->data);
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The client could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->Client->recursive = 0;
        $this->data = $this->Client->read(null, $id);
    }
    $statuses = $this->Client->Status->find('list');
    $this->set(compact('statuses'));
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜