2010-12-12 70 views
0

我創建了一個cakephp應用程序,它帶有一個生成默認值的表單(現在只有佔位符數據,直到獲得解析)纔會被拋入表中。表單工作,它插入記錄罰款,但由於某種原因,表單只使用默認值的第一個字符。因此,例如,「標題」只在表格中具有字符「t」,「內容」僅具有「c」等。cakephp只使用第一個字符的默認值

當我pr($ this-> data)時,所有佔位符數據都在那裏。我可以編輯它們並添加更多保存得很好的文本,所以它不是表單字段長度問題。在$ this-> data和$ this-> Form-> input之間的某處,默認值將被截斷。我不知道從哪裏開始解決這個問題。我在這裏找不到任何東西,我只能通過使用Google搜索找到這個問題的一個提及,這個問題沒有解決。

CakePHP的1.3.6,5.3.3 PHP,Linux的

感謝您的幫助

公關的結果($這個 - >數據):

Array 
(
    [title] => title 
    [content] => content 
    [media_url] => media_url 
) 

查看:

<? pr($this->data); ?> 

<div class="generators form"> 
<?php echo $this->Form->create('Generator');?> 
    <fieldset> 
     <legend>Create New Post</legend> 
    <?php 
     echo $this->Form->input('title'); 
     echo $this->Form->input('content'); 
     echo $this->Form->input('publish_date'); 
     echo $this->Form->input('media_url'); 

    ?> 
    </fieldset> 
<?php echo $this->Form->end('Create Post');?> 
</div> 

控制器:

<?php 
class GeneratorsController extends AppController { 

    var $name = 'Generators'; 

    function posts() 
    { 
     // save the post 
     if (!empty($this->data)) { 
      $this->Generator->create(); 
      if ($this->Generator->save($this->data)) { 
       $this->Session->setFlash(__('The post has been created', true)); 
       $this->redirect(array('action' => 'posts')); 


       // TODO: call posting app 


      } else { 
       $this->Session->setFlash(__('There was a problem. Please, try again.', true)); 
      } 
     } 
     else 
     { 
       // create post 
       $this->data['title'] = "title"; 
       $this->data['content'] = "content"; 
       //$this->data['publish_date'] = ""; 
       $this->data['media_url'] = "media_url"; 


     } 
    } 
} 
?> 

型號:

<?php 
class Generator extends AppModel { 
    var $name = 'Generator'; 
    var $displayField = 'title'; 
} 
?> 

回答

1

在下面的行:

<?php echo $this->Form->create('Generator'); ?> 

第一個參數表示,其形式所屬的模型。這將這些字段的名稱格式化爲data[Generator][field_name]。所以,在設置你的數據的同時,你需要照顧這個:

function posts() { 
    if (!empty($this->data)) { 
     ... 
    } else { 
     $this->data = array(
      'Generator' => array(
       'title' => 'title', 
       'content' => 'content', 
       'media_url' => 'media_url' 
      ) 
     ); 
    } 
} 

讓我知道這是否工作。

+0

賓果,感謝您的超級快速響應。不會再犯這個錯誤 – 2010-12-12 03:05:51

+0

@Jon - 很高興知道它工作:) – RabidFire 2010-12-12 03:54:38

相關問題