我想保存数据,我从表单中获取。
我的错误讯息:
错误:在非对象上调用成员函数dirty()。
我的观看代码:
<?= $this->Form->create($album,['role' => 'form','type' => 'file'])?>
<div class="form-group">
<?= $this->Form->input( 'albumName',
[ 'class' => 'form-control',
'label' => __('Albüm Adı:'),
'placeholder' => __('Albüm Adı'),
'onkeyup' => 'javascript:setVirtualSanalUrl(this,"virtualUrl")']);?>
</div>
<div class="form-group">
<?= $this->Form->input( 'albumVirtualUrl',
[ 'class' => 'form-control',
'label' => __('Albüm Sanal Adres(Site Adres Bilgisi İçin):'),
'placeholder' => __('Albüm Sanal Adres'),
'id' => 'virtualUrl',
'onChange' => 'javascript:setVirtualSanalUrl(this,"virtualUrl")']);?>
</div>
<?php
foreach ($Languages as $lang):?>
<?php echo $lang;?>
<div class="form-group">
<?= $this->Form->input( '_translations.'.$lang.'.albumName',
[ 'class' => 'form-control',
'label' => __('Albüm Adı({0}):', [$lang]),
'placeholder' => __('Albüm Adı({0})', [$lang]),
'onkeyup' => 'javascript:setVirtualSanalUrl(this,"virtualUrl'.$lang.'")']);?>
</div>
<div class="form-group">
<?= $this->Form->input( '_translations.'.$lang.'.albumVirtualUrl',
[ 'class' => 'form-control',
'label' => __('Albüm Sanal Adres(Site Adres Bilgisi İçin)({0}):', [$lang]),
'placeholder' => __('Albüm Sanal Adres({0})', [$lang]),
'id' => 'virtualUrl'.$lang,
'onChange' => 'javascript:setVirtualSanalUrl(this,"virtualUrl'.$lang.'")']);?>
</div>
<?php
endforeach;
?>
<div class="box-footer">
<?= $this->Form->button(isset($album->id) == false ? __('Kaydet'):__('Düzenle'),
['class' => 'btn btn-primary']);?>
<?= $this->Form->end();?>
数据是我放置的记录:
$album = $this->ImageAlbums->newEntity();
if ($this->request->is('post')) {
$album = $this->ImageAlbums->patchEntity($album, $this->request->data);
$album->albumVirtualUrl = $this->createVirtaulUrl($album->albumVirtualUrl);
if (empty($album->errors())) {
$resultAlbum = $this->ImageAlbums->save($album);}}
此代码无效,但下面的代码工作正常。
$example = $this->ImageAlbums->newEntity();
$example->albumName = "deneme";
$example->albumVirtualUrl = "sdsd";
$example->translation('en-US')->albumName = 'albumName';
$example->translation('en-US')->albumVirtualUrl = "albumVirtualUrl";
$example->translation('fr_FR')->albumName = "fr albumName";
$example->translation('fr_FR')->albumVirtualUrl = "fr albumVirtualUrl";
$this->ImageAlbums->save($example);
我查看了这些页面:http://book.cakephp.org/3.0/en/orm/behaviors/translate.html
我做了一些步骤。你可以直接记录数据,但我无法注册论坛。
答案 0 :(得分:0)
我知道我一年太晚但我面临同样的问题。 dirty()方法期望一个对象,而不是一个数组。 因此,您可以像手动一样手动分配翻译。 此脚本应该可以解决问题:
foreach ($this->request->data['_translations'] as $k => $data) {
foreach ($data as $dataKey => $datum) {
$yourEntity->translation($k)->{$dataKey} = $datum;
}
}
$this->Model->save($yourEntity);