如何在cakePHP中设置编辑和添加具有hasOne关系的视图?

时间:2011-04-15 16:30:14

标签: cakephp plugins view media has-one

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

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

我检查了蛋糕文件
http://book.cakephp.org/#!/view/1032/Saving-Related-Model-Data-hasOne-hasMany-belongsTo阅读Media Plugin中的大多数文档,这是最相关的 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,
));
[...]
?>

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

查看 - 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,
  `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`)
) 

2 个答案:

答案 0 :(得分: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。在数据库中查找外键并在此处输入名称。

答案 1 :(得分:0)

感谢所有回复。我终于搞定了。似乎出于某种原因,saveAll让我感到悲伤。当我保存客户端然后保存Logo一切正常。

我发布了以下代码。

控制器代码

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'));
}