0

我想知道是否有人可以帮助我。

我正在使用 cakePHP 1.3,但无法让编辑视图更新主模型和 hasOne 相关模型。我相当肯定这与我对 edit.ctp 视图的设置有关。我正在使用我在另一个模型上工作的媒体插件,所以我认为这与此无关。具体来说,我正在努力让 Media Plugin,Monolithic Attachment Model 与 hasOne 关系正常工作。

我已经查看了蛋糕文档
http://book.cakephp.org/#!/view/1032/Saving-Related-Model-Data-hasOne-hasMany-belongs
阅读媒体插件中的大部分文档,就像是最相关的
https://github.com/davidpersson/media/blob/next/docs/RECIPES
并花费大量时间搜索谷歌。

任何帮助,将不胜感激。

谢谢,

詹姆士

模型 - 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,
));
[...]
?>

控制器 - 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'));
}
    [...]
    ?>

查看-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>

客户端 sql

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

附件 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`)
) 
4

2 回答 2

0
    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,
));

在这里,您还没有定义将您的徽标绑定到客户端的 foreign_key。在您的数据库中查找外键并将其名称放在这里。

于 2011-04-15T21:26:31.207 回答
0

感谢所有的回复。我终于让它工作了。似乎出于某种原因, saveAll 让我很伤心。当我保存客户端然后保存徽标时,一切正常。

我在下面发布了代码。

控制器代码

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'));
}
于 2011-04-17T15:52:46.347 回答